Laravel更新关联表

时间:2017-05-16 08:02:39

标签: php laravel

用户模型:

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

User_Information模型:

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

我可以更新该函数中的用户表列,但我无法访问和更新任何user_information列。

我该如何更新?

users table的id = user_information表的user_id ....

P.S。我不知道雄辩的工作原则,我正在与Doctrine合作。

3 个答案:

答案 0 :(得分:1)

由于您的所有输入都与数据库字段具有相同的名称,因此您可以使用质量分配。但请务必在模型中设置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',
])));

答案 1 :(得分:0)

尝试这样做

$userInformation = $user->userInformation;
$userInformation->title = $request->input('title');

来源:https://laravel.com/docs/5.3/eloquent-relationships#one-to-one

不要忘记保存这样的用户信息

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

答案 2 :(得分:0)

您可以单独填充UserInformation

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

或者,您可以使用associate()方法更新belongsTo关系。

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