gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:15 25 4
gpt4 key购买 nike

我是这个框架的新手,我有一个问题。我进行了登录身份验证以访问,当我转到我的个人资料页面时,我可以查看我的数据并在同一页面上进行所有更改。我需要去获取我的 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() !!}

我的 Controller :

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 步:编写路线

Route::post('updateProfile', 'homeController@updateProfile');

它将调用homeControllerupdateProfile函数

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

这是给你的简单/示例函数

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

这里是Validator,你应该修改它

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

第四步:返回结果

如果验证通过,那么它将在表中更新到传递了 id 的用户,即 Input::get('id') ,我们将返回页面return view('profile')->with('message', '更新成功');
我认为你的页面名称为profile.blade.php,你应该根据你的 Blade 更改它,

如果验证失败,则返回错误信息页面

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 调用来传递变量并显示/隐藏 Controller 返回的结果

希望对你有帮助

关于php - 在同一页面上更新用户 laravel 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com