0
0
Laravelframework~5 mins

Query scopes in Laravel - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a query scope in Laravel?
A query scope is a way to define common query logic inside a model. It lets you reuse query conditions easily by calling a method on the model's query builder.
Click to reveal answer
beginner
How do you define a local query scope in a Laravel model?
Define a method in the model starting with scope followed by the scope name. For example, scopeActive($query) defines a scope called active. The method receives the query builder as the first argument.
Click to reveal answer
beginner
How do you use a local query scope in Laravel when querying?
Call the scope method name without the scope prefix on the model query. For example, User::active()->get() uses the scopeActive method.
Click to reveal answer
intermediate
What is a global query scope in Laravel?
A global query scope automatically adds conditions to all queries for a model. It is useful for things like soft deletes or multi-tenant filtering.
Click to reveal answer
intermediate
How do you remove a global scope from a Laravel query?
Use the <code>withoutGlobalScope()</code> method on the query builder and pass the scope class or name to exclude it from the query.
Click to reveal answer
How do you name a local query scope method in a Laravel model?
AStart with 'scope' followed by the scope name
BStart with 'query' followed by the scope name
CUse any method name without prefix
DStart with 'filter' followed by the scope name
How do you apply a local query scope named 'active' when querying a model?
AUser::scopeActive()->get()
BUser::active()->get()
CUser::filterActive()->get()
DUser::whereActive()->get()
What is the main purpose of a global query scope?
ATo handle database migrations
BTo define reusable query methods manually called
CTo create database indexes
DTo add conditions to all queries automatically
How can you exclude a global scope from a query?
AUse withoutGlobalScope() method
BUse removeScope() method
CUse ignoreScope() method
DUse skipScope() method
Which argument does a local query scope method receive first?
AThe model instance
BThe database connection
CThe query builder
DThe request object
Explain how to create and use a local query scope in Laravel.
Think about naming and how you call the scope when querying.
You got /3 concepts.
    Describe what a global query scope is and how to remove it from a query.
    Consider automatic query changes and how to opt out.
    You got /3 concepts.