Laravel

Eloquent Advanced

Query scopes, local scopes, global scopes, observers, events, collections methods, custom casts, attribute casting

24 คำถามสัมภาษณ์·
Senior
1

What is a local scope in Eloquent?

คำตอบ

Local scopes allow defining reusable query constraints in the model. They are declared with the scope prefix and called without it. This approach improves code readability and prevents duplication of filtering logic throughout the application.

2

How to define a local scope named 'active' in an Eloquent model?

คำตอบ

A local scope is defined with the scope prefix followed by the name in PascalCase. The method receives the query builder object and returns the same modified object. The scope is then called without the prefix, in camelCase, directly on the query builder.

3

What is a global scope in Eloquent?

คำตอบ

A global scope automatically applies a constraint to all queries on a model. It registers in the model's boot method and implements the Scope interface. This feature is useful for systematic filters like soft delete or multi-tenancy.

4

How to temporarily remove a global scope from a query?

5

What is a model observer in Laravel?

+21 คำถามสัมภาษณ์

เชี่ยวชาญ Laravel สำหรับการสัมภาษณ์ครั้งถัดไป

เข้าถึงคำถามทั้งหมด flashcards แบบทดสอบเทคนิค แบบฝึกหัด code review และตัวจำลองสัมภาษณ์

เริ่มใช้ฟรี