
Eloquent ORM Basics
Models, CRUD operations, query builder, mass assignment, timestamps, soft deletes, accessors, mutators
1What naming convention does Eloquent use by default to deduce the table name from the model name?
What naming convention does Eloquent use by default to deduce the table name from the model name?
답변
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.
2Which Eloquent method allows creating AND saving a new model in a single operation?
Which Eloquent method allows creating AND saving a new model in a single operation?
답변
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.
3What happens if attempting to mass assign an attribute that is not in $fillable?
What happens if attempting to mass assign an attribute that is not in $fillable?
답변
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.
What is the difference between the find() and findOrFail() methods in Eloquent?
How does Eloquent automatically handle the created_at and updated_at columns?
+19 면접 질문