在同一页面上更新用户 laravel 5

Update user on the same page laravel 5

我是这个框架的新手,我有一个问题。 我进行了登录身份验证以访问,当我转到我的个人资料页面时,我可以查看我的数据并在同一页面上进行所有更改。 我需要去获取我的 ID 才能进行更新还是不需要? 我需要做什么? 我做的方式好吗?我只需要创建一个 route::post?

我的个人资料页面:

@if(Session::has('message'))
    <div class="alert alert-danger">
    <h5>{{ Session::get('message') }}</h5>
    </div>
@endif

{!! Form::open(array('url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form'))!!}

<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('name', 'Utilizador', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::text('name', null, ['class' => 'form-control input-md', 'placeholder' => 'Utilizador']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('nascimento', 'Data de nascimento', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::date('nascimento', null, ['class' => 'form-control input-md']) !!}
        {{ $errors->first('nascimento', '<span class=error>:message</span>') }}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('sexo', 'Sexo', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::select('sexo', ['Masculino', 'Feminino'], null, ['class' => 'form-control input-md']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('email', 'Email', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::text('email', null, ['class' => 'form-control input-md', 'placeholder' => 'Email']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('password', 'Password', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::password('password', ['class' => 'form-control input-md', 'placeholder' => 'Password']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('rpassword', 'Confirmar password', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::password('rpassword', ['class' => 'form-control input-md', 'placeholder' => 'Confirmar password']) !!}
    </div>
</div>                          
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::file('imagem', ['class' => 'input-file']) !!}
    </div>
</div>   
<div class="row" style="margin-bottom: 20px; margin-top: 30px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-9 col-lg-9">
        {!! Form::submit('Enviar', ['class' => 'btn btn-primary']) !!}
    </div>
</div> 
{!! Form::close() !!}

我的控制器:

public function perfil() {
    return view('backend/perfil.index');        
}           

public function updateProfile() {

    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        User::where('id', Input::get('id'))->update($profileData);
        return view('backend/perfil.index')->with('message', 'Updated Succesfully');
    } else {
        return view('backend/perfil.index')->with('message', $validation->messages());
    }
}

我的路线:

Route::get('backend/perfil','BackendControlador@perfil');
Route::post('backend/perfil', 'BackendControlador@updateProfile');

我的应用用户:

public static $profileData = array(
        'email' =>  'required|email',
        'name' =>  'required',
        );

这是您想要执行的详细操作。

第 1 步: 打开表格

{!! Form::open(array('url' => 'updateProfile', 'name' => 'updateProfile', 'role' => 'form'))!!}

注意:您的表单方法操作是空的。您应该查看您的来源以查看它

第 2 步:写路由

路线::post('updateProfile', 'homeController@updateProfile');

它将调用 homeController's updateProfile 函数

第 3 步:定义控制器,验证输入并通过模型完成您的操作

这是为您准备的 simple/sample 功能

public function updateProfile()
    {
        $profileData = Input::except('_token');
        $validation = Validator::make($profileData, User::$profileData);
        if ($validation->passes()) {
            User::where('id', Input::get('id'))->update($profileData);
            return view('profile')->with('message', 'Updated Succesfully');
        } else {
            return view('profile')->with('message', $validation->messages());
        }

    }

它所做的是获取除_token以外的所有输入并将其存储在$profileData中,然后它将在$profileData中进行验证19=]型号

这里是Validator,你要修改它

public static $profileData = array(
        'email' =>  'required|email',
        'name' =>  'required',
        );

第 4 步:Return 结果

如果验证通过,那么它会在 table 中更新到用户 ID 被传递的位置,即 Input::get('id') ,我们将 return 带有 return view('profile')->with('message', 'Updated Succesfully'); 我认为你的页面名称是profile.blade.php,你应该根据你的blade、

更改它

如果验证失败,那么我们将return带有错误信息的页面

return view('profile')->with('message', $validation->messages());

你应该在你的 blade 中有这个来显示你的错误信息

@if(Session::has('message'))
    <div class="alert alert-danger">
    <h5>{{ Session::get('message') }}</h5>
    </div>
@endif

注:

如果您不想刷新页面,那么您只需执行一个 Ajax 调用来传递变量和 show/hide 结果,即由控制器 return

希望对您有所帮助