Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
Laravel fill not setting component property

I have run into an issue with Laravel that hopefully someone could help explain. The fill command is not setting the properties of the model, and instead can only be used to save them to the database with the ->save() function. Is there a way to do fill where it actually sets the properties of the model from an array.

Example of problem below.

$comment = new comment();
$comment->fill(['name' => 'Bob']);
echo($comment->name); // Gives null/error.
$comment = new comment();
$comment->name = 'Bob';
echo($comment->name); // Gives Bob.
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The reason is name filed is not fillable in comment model .To Solve this issue you have to add fillable for that field in comment model

protected $fillable = [
        'name',

    ];

If you try to access non fillable field then it will return null.

If don't want to add it in fillable then you can use forceFill.

forceFill: Fill the model with an array of attributes with force mass assignment.

$comment = new comment();
$comment->forceFill(['name' => 'Bob']);
echo($comment->name);
over 4 years ago · Santiago Trujillo Report

0

Yes, you need to use fillable property of the model and need to add name inside fillable array. That's the recommended way to do it.

As suggested by John Lobo's answer, You can forcibly override that mechanism using forceFill or forceCreate. Both ignores fillable property in the model. It may possible that you may have a column like is_admin and you do not want to update it but force will do it.

Notes :

  1. The main problem with forceCreate or forceFill, is that we have to manage the foreign key assignment manually.

  2. forceFill will do fill (properties) only. You need save method call to save the actual record while forceCreate will do fill (properties) and Save both together.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!