Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda