Why is $cv in this case

$cv = Cv::findOrFail($id)->fill($request->all());
$cv->save();
var_dump($cv);

returning the model (so I can access e.g. $cv->id

and in this case

$cv = Cv::findOrFail($id)->fill($request->all())->save();
var_dump($cv);

only returning true?

2 Answers

0
blackpla9ue On Best Solutions
$cv = Cv::findOrFail($id)->fill($request->all());

Here, your last chained method fill returns a model object, which is assigned to your variable $cv.

$cv->save();

This returns a boolean but you haven't assigned it to $cv, so your $cv variable is still the same as the first line, which is the model.

In your next example,

$cv = Cv::findOrFail($id)->fill($request->all())->save();

Your last chained method save returns a boolean, which is assigned to your variable $cv

3
Jimmy Zoto On

I was confused by something similar. The methods fill() and create() both create and return a new model. You need to get a handle on the model before you can do anything with it.

The method save() always returns boolean.

So in the second case, the model is being created, and save is being called on that new model (check the DB to make sure), but you have no way of accessing it.

Although it's not as pretty, the first case is how you should create/fill models.