Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda