gpt4 book ai didi

php - 如何更新用户资料? Laravel-5

转载 作者:可可西里 更新时间:2023-11-01 13:27:47 25 4
gpt4 key购买 nike

首先我想说我不知道​​我在做什么...我有一个 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();
});

我在创建现在看起来像这样的更新 Controller 时遇到了问题。

public function update(Request $request)
{

$user = $request->user();
$data['description'] = $request->input('description');
$data['experience']=$request->input('experience');

$user->user_info -> $data->save();
}

再次...不知道我在做什么...

这是我的表格:

    <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>

更新:我可以这样更新它:

$user->user_info->description = $data['description'];
$user->user_info->experience = $data['experience'];
$user->user_info->save();

但有没有办法让我做类似的事情:

$user->user_info->$request::Input::all();
$user->user_info->save();

最佳答案

试试这个:

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);
}
}

还在您的 UserInfo 模型中添加:

protected $fillable = array('description', 'experience');
public function getFillable() {
return $this->fillable;
}

关于php - 如何更新用户资料? Laravel-5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32794187/

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