Laravel

Eloquent ORM Basics

Models, CRUD operations, query builder, mass assignment, timestamps, soft deletes, accessors, mutators

22 perguntas de entrevistaยท
Junior
1

What naming convention does Eloquent use by default to deduce the table name from the model name?

Resposta

Eloquent uses the plural snake_case of the class name to deduce the table name. For example, the User model corresponds to the users table, and BlogPost corresponds to blog_posts. This convention can be overridden by defining the $table property in the model.

2

Which Eloquent method allows creating AND saving a new model in a single operation?

Resposta

The create() method allows creating and saving a model in a single operation by accepting an array of attributes. It requires attributes to be listed in the $fillable property or $guarded to be defined to protect against mass assignment. Unlike new Model() followed by save(), create() combines these two steps.

3

What happens if attempting to mass assign an attribute that is not in $fillable?

Resposta

Laravel throws a MassAssignmentException to protect against mass assignment vulnerabilities. This protection prevents malicious users from injecting unauthorized attributes via HTTP requests. You must either add the attribute to $fillable, use $guarded to protect only certain attributes, or use forceFill() to bypass protection.

4

What is the difference between the find() and findOrFail() methods in Eloquent?

5

How does Eloquent automatically handle the created_at and updated_at columns?

+19 perguntas de entrevista

Domine Laravel para sua proxima entrevista

Acesse todas as perguntas, flashcards, testes tecnicos, exercicios de code review e simuladores de entrevista.

Comece gratis