在同一页面更新用户laravel 5


Update user on the same page laravel 5

我是这个框架的新手,我有一个问题。我进行了登录验证以访问,当我转到我的个人资料页面时,我可以在同一页面上查看我的数据并更改所有内容。我需要去拿我的ID才能进行更新还是不需要?我需要做什么?我的表现还好吗?我只需要创建一个路由::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'updateProfile函数

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

以下是的简单/示例功能

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中,然后在User型号中的$profileData中进行定义的验证

这是验证器,你应该修改它

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

步骤4:返回结果

如果验证通过,那么它将在表中更新到id通过的用户所在的位置,即Input::get('id'),我们将返回带有return view('profile')->with('message', 'Updated Succesfully'); 的页面我认为你的页面名称是profile.blade.php,你应该根据你的刀片进行更改

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

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

你应该在你的刀片中有这个来显示你的错误消息

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

注意:

如果你不想刷新页面,那么你只需要做一个Ajax调用来传递变量,并显示/隐藏控制器返回的结果

希望这能帮助您