Reputation: 4055
I am trying to update only one field on a models relation but it keeps returning it as null and I cant figure out why;
The profile
is a belongsTo
relationship
$user = User::find(1);
$user->profile->api_key = 'apikeyhere';
$user->save();
After i do this and then run dd($user->profile)
.. The api_key field is null instead of the actual value.
Upvotes: 2
Views: 4194
Reputation: 13
Update the field using the relation.
$user = User::find(1);
$user->profile()->update(['api_key' => 'api_key value']);
Upvotes: 0
Reputation: 1583
Using two query
$user = User::find(1);
$user->profile()->update([ 'api_key' => 'apikeyhere']);
Using One query
Profile::where('user_id', 1)->update([ 'api_key' => 'apikeyhere']);
Upvotes: 5