BigJobbies
BigJobbies

Reputation: 4055

Laravel update one field of a model relation

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

Answers (2)

T_nishan
T_nishan

Reputation: 13

Update the field using the relation.

$user = User::find(1);
$user->profile()->update(['api_key' => 'api_key value']);

Upvotes: 0

JS TECH
JS TECH

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

Related Questions