RogerManich's avatar

Model update doesn't work.

Hi, I am following the course "Laravel in 30 days". In day 18 shows two ways to update a record: a) Using save method after updating each field. It works fine.

$job = Job::findOrFail($id);
    //Option1
    $job->Title = request("Title");
    $job->Salary = request("Salary");
    $job->save();

b) Using the method update by passing an array.

$job = Job::findOrFail($id);
   
    $job->update([
        'Title' => request('Title'),
        'Salary' => request('Salary')
    ]);

The second one return true but no update has been done. I dump $job in the second option. I don't know if I miss something.

0 likes
6 replies
JaredF's avatar

This may be a mass assignment issue, make sure the fields are either in the model's fillable array, or ensure they are not in the model's guarded array.

1 like
RogerManich's avatar

@JaredB you right! I review my model and I had fillable array set but with uppercase issue.

Wrong

protected $fillable = ["employer_id","title", "salary"];

Fixed (Same as my Db definition). Consistency matters as Jeffrey says. :-)

protected $fillable = ["employer_id","Title", "Salary"];

Thank again to point me to the right track

Snapey's avatar

As Jeff says, consistency matters so you wont have picked up this from Jeffrey.

Uppercase first letter should be reserved for class names

RogerManich's avatar

@Snapey , as I said I agree. It is what I usually do in my projects. I don't know why I used this wrong convention and I didn't changed. It is just a source code from a course. Sometimes it is fine handling wrong code to discover issues like this. :-) Erros are the key to learn better ;)

1 like

Please or to participate in this conversation.