Laravel

Eloquent Advanced

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

24 Interview-Fragen·
Senior
1

What is a local scope in Eloquent?

Antwort

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?

Antwort

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?

Antwort

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 Interview-Fragen

Meistere Laravel für dein nächstes Interview

Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.

Kostenlos starten