Laravel

Eloquent ORM Basics

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

22 câu hỏi phỏng vấn·
Junior
1

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

Câu trả lời

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?

Câu trả lời

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?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Laravel cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí