Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将质量分配与两个模型一起使用?_Php_Laravel_Laravel 7 - Fatal编程技术网

Php 如何将质量分配与两个模型一起使用?

Php 如何将质量分配与两个模型一起使用?,php,laravel,laravel-7,Php,Laravel,Laravel 7,我有一个用户模型(user)。我还为这个用户提供了一个配置文件模型(profile) 我想做什么;在ProfileController@update方法。但我不能完全理解这个问题。这样做的最佳实践是什么?我不知道是否有我没有看过的文章。。。如果你能举个例子给我解释一下,我会很高兴的 事先非常感谢 以下是一些代码: ProfileController@update public函数更新(UpdateUserRequest$request,\App\Models\Profile$Profile) {

我有一个用户模型(user)。我还为这个用户提供了一个配置文件模型(profile)

我想做什么;在
ProfileController@update
方法。但我不能完全理解这个问题。这样做的最佳实践是什么?我不知道是否有我没有看过的文章。。。如果你能举个例子给我解释一下,我会很高兴的

事先非常感谢

以下是一些代码:

ProfileController@update

public函数更新(UpdateUserRequest$request,\App\Models\Profile$Profile)
{
$request->authorize('update',$request->profile);
//获取所有数据
$data=$request->EXPECT([''令牌',''方法','密码确认']);
如果($data['password']==null){
未设置($data['password']);
}
$profile->user($data);
$profile->update($data);
吐司(Profil bilgileriniz başarıyla güncellendi),成功);
返回重定向(显示路由($profile));
}
App\Models\Profile.php

类概要文件扩展了模型
{
使用ImageableModel、ModelStatus;
受保护的$fillable=[
“全名”,
“经验”,
“生日”,
“国家”,
"地位",,
“语言代码”,
];
受保护的$casts=[
“生日”=>“日期时间”,
];
公共函数用户()
{
返回$this->belongsTo(用户::类);
}
}
App\Models\User.php

类用户扩展可验证的实现
核对电子邮件
{
使用应呈报文件;
受保护的$fillable=[
“用户名”,
“电子邮件”,
“密码”,
];
受保护的$hidden=[
“密码”,
“记住你的代币”,
];
受保护的$casts=[
'电子邮件在'=>'日期时间'>,
];
公共职能简介()
{
返回$this->hasOne(Profile::class,'id');
}
}

为什么不执行以下操作

$profile->fullname=$data['fullname'];
$profile->experience=$data['experience'];
$profile->birthday=$data['birthday'];
$profile->country=$data['country'];
$profile->status=$data['status'];
$profile->lang_code=$data['lang_code'];
$profile->save();
$profile->user->username=$data['username'];
$profile->user->email=$data['email'];
$profile->user->save();

谢谢您的回答@Franquis。实际上,我喜欢
$model->update($request->all())更多更好。因为它很短。:)但是,在我找到更好的解决方案之前,我正在使用您编写的版本。