Php Laravel更新关联表

Php Laravel更新关联表,php,laravel,Php,Laravel,用户模型: class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attri

用户模型:

class User extends Authenticatable {
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function userInformation() {
        return $this->hasOne('App\UserInformation', 'user_id', 'id');
    }
}
用户信息模型:

class UserInformation extends Model {

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = "user_information";

    public function users() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }
}
以下是更新部分:

public function updateUser($request, $id) {
        $user = User::findOrFail($id);
        if (!empty($request->input('email'))) $user->email = $request->input('email');
        if (!empty($request->input('password'))) $user->password = $request->input('password');
        if (!empty($request->input('type')) || $request->input('type') === '0') $user->type = $request->input('type');
        if (!empty($request->input('package_size'))  || $request->input('package_size') === '0') $user->package_size = $request->input('package_size');
        if (!empty($request->input('package_expiration_date'))) $user->package_expiration_date = $request->input('package_expiration_date');
        if (!empty($request->input('is_deleted')) || $request->input('is_deleted')) $user->is_deleted = $request->input('is_deleted');

        if (!empty($request->input('title'))) $user->userInformation()->title = $request->input('title');
        if (!empty($request->input('first_name'))) $user->userInformation()->name = $request->input('first_name');

        $user->save();

        return $user;
    }
我可以在该函数中更新用户表列,但不能访问和更新任何用户信息列

如何更新

用户表的id=用户信息表的用户id

p、 我不知道雄辩的工作原理,我是在用教条工作。

试着这样做

$userInformation = $user->userInformation;
$userInformation->title = $request->input('title');
资料来源:

别忘了像这样保存用户信息

$user->userInformation()->save($userInformation);

由于所有输入与数据库字段具有相同的名称,因此可以使用批量分配。但一定要在模型中设置fillable属性。或者,您可以只提取所需的输入并进行更新。以下代码仅在提供与数据库字段对应的输入时更新,否则将忽略这些代码

//User
protected $fillable = [
    'name', 'email', 'password', 'type', 'package_size', 'package_expiration_date', 'is_deleted'
];

//UserInformation
protected $fillable = [
    'title', 'first_name'
];

$user->update(array_filter($request->all()));

$user->userInformation()->update(array_filter($request->only(['title','first_name'])));

// OR

$user->update(array_filter($request->only([
    'email',
    'password',
    'type',
    'package_size',
    'package_expiration_date',
    'is_deleted',
])));

$user->userInformation()->update(array_filter($request->only([
    'title',
    'first_name',
])));
您可以单独填充用户信息

 $user = User::findOrFail($id);
 $user->email = $request->input('email');
 ...
 if($user->save())
 {
    $userInfo = App\UserInformation::where('user_id',$id);
    $userInfo->title = $request->input('title');
    $userInfo->name = $request->input('first_name');
    $userInfo->save();
 }
或者,您可以使用方法更新belongsTo关系

$userInfo->user()->associate($user);
$userInfo->save();

如果我想修补怎么办?这样行吗?对于我的代码,如果我发送到空参数,我不会更新。上面的代码用于更新/修补。这与更新少数字段或所有字段相同。如果您只提供电子邮件和密码,则仅更新这些字段。您可以测试代码并亲自查看。是的,如果您发送一个空输入,则将跳过更新。我以前使用的方法是您在开始时使用的,它太麻烦了。这正好做到了这一点,而且非常简短: