如何更新用户资料?Laravel-5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32794187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I update user profile? Laravel-5
提问by Hymanjoesmith
Just want to start by saying I have no clue what I'm doing... I have a user_info table that looks like this
只是想首先说我不知道我在做什么......我有一个看起来像这样的 user_info 表
Schema::create('user_info', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('address');
$table->string('city');
$table->string('state');
$table->string('zip');
$table->text('description');
$table->text('experience');
$table->timestamps();
});
I'm having trouble creating the update controller which looks like this right now.
我在创建现在看起来像这样的更新控制器时遇到了问题。
public function update(Request $request)
{
$user = $request->user();
$data['description'] = $request->input('description');
$data['experience']=$request->input('experience');
$user->user_info -> $data->save();
}
again...no clue what I'm doing...
再次......不知道我在做什么......
and this be my form:
这是我的表格:
<div class='col-md-10 well form-well'>
{!! Form::open(['method' => 'PATCH', 'action'=> ['UserController@update', Request::user()->id]]) !!}
<div class='row'>
<div class='form-group'>
<div class='col-md-2'>
{!! Form::label('description', 'About You')!!}
</div>
<div class='col-md-7'>
{!! Form::textarea('description', null, ['class'=>'form-control', 'rows'=>'3'])!!}
</div>
</div>
</div>
<div class='row'>
<div class='form-group'>
<div class='col-md-2'>
{!! Form::label('experience', 'Experience and Skills')!!}
</div>
<div class='col-md-7'>
{!! Form::text('experience', null, ['class'=>'form-control'])!!}
</div>
</div>
</div>
<div class='form-group'>
{!! Form::submit('Save Changes',['class'=> 'btn btn-md btn-success']) !!}
{!! Form::close()!!}
</div>
Update: I was able to update it like this:
更新:我能够像这样更新它:
$user->user_info->description = $data['description'];
$user->user_info->experience = $data['experience'];
$user->user_info->save();
But is there a way I can do something like :
但是有没有办法我可以做这样的事情:
$user->user_info->$request::Input::all();
$user->user_info->save();
采纳答案by num8er
Try this:
尝试这个:
public function update(Request $request, $id)
{
$User = User::with('user_info')->find($id);
if(!$User) {
return response('User not found', 404);
}
$UserInfo = $User->user_info;
if(!$UserInfo) {
$UserInfo = new UserInfo();
$UserInfo->user_id = $id;
$UserInfo->save();
}
try {
$values = Input::only($UserInfo->getFillable());
$UserInfo->update($values);
} catch(Exception $ex) {
return response($ex->getMessage(), 400);
}
}
also in Your UserInfo model add this:
同样在您的 UserInfo 模型中添加:
protected $fillable = array('description', 'experience');
public function getFillable() {
return $this->fillable;
}