Cowgirl
Cowgirl

Reputation: 1494

Laravel Eloquent cannot assign a value to the model's relation property

I am unable to assign a value to a model's property

App\person::find(1)->plan

This command gives me something like this in artisan tinker

     id: 1,
     name: "name",
     description:"",

I am trying to specify a value for description

App\person::find(1)->plan->description='something'

It outputs

'something'

But it's not saving it in the description, I still get this

     id: 1,
     name: "name",
     description:"",

I tried several commands, none work

App\person::find(1)->plan->description='something'->save();
App\person::find(1)->plan->description->save('something');

Am I doing it wrong? Yes, the model's property has been set to fillable

Upvotes: 1

Views: 1316

Answers (2)

To save you have to

$person= App\person::find(1);
$person->plan->description='something';
$person->plan->save();

Or if you need a one liner do

App\person::find(1)->plan->fill(['description' => 'something')->save();

.

The way you did will try to execute save on a string:

App\person::find(1)->plan->description='something'->save();

Upvotes: 5

Nartub
Nartub

Reputation: 3908

Try like this:

$plan = App\person::find(1)->plan;
$plan->description = 'something';
$plan->save();

Upvotes: 0

Related Questions