0
0
Laravelframework~20 mins

Query scopes in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Scopes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel query scope usage?
Given the following Laravel model and query, what will be the SQL query generated and executed?
Laravel
class Post extends Model {
    public function scopePublished($query) {
        return $query->where('published', true);
    }
}

$posts = Post::published()->toSql();
A"select * from `posts` where `published` = 1"
B"select * from `posts` where `published` = true"
C"select * from `posts` where `published` = 'true'"
D"select * from `posts` where `published` = '1'"
Attempts:
2 left
💡 Hint
Remember how Laravel converts boolean values in queries.
state_output
intermediate
2:00remaining
What is the value of $users after applying this scope?
Consider this Laravel model and query. What users will be included in $users?
Laravel
class User extends Model {
    public function scopeActive($query) {
        return $query->where('active', 1);
    }
}

$users = User::active()->get();
ANo users, because scope is not applied correctly
BAll users with 'active' column set to true (boolean)
CAll users regardless of 'active' status
DAll users with 'active' column set to 1
Attempts:
2 left
💡 Hint
Think about how Laravel treats integer 1 in queries.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a local query scope in Laravel?
Choose the correct syntax for defining a local query scope named 'recent' in a Laravel model.
Apublic static function scopeRecent($query) { return $query->orderBy('created_at', 'desc'); }
Bpublic function recent($query) { return $query->orderBy('created_at', 'desc'); }
Cpublic function scopeRecent($query) { return $query->orderBy('created_at', 'desc'); }
Dpublic function recentScope($query) { return $query->orderBy('created_at', 'desc'); }
Attempts:
2 left
💡 Hint
Local scopes must start with 'scope' and be instance methods.
🔧 Debug
advanced
2:00remaining
Why does this query scope not filter results as expected?
Given this scope and usage, why does the query return all records instead of filtered ones?
Laravel
class Product extends Model {
    public function scopeInStock($query) {
        $query->where('stock', '>', 0);
    }
}

$products = Product::inStock()->get();
AThe query builder does not support where clauses in scopes.
BThe scope method does not return the query, so the filter is not applied.
CThe scope method should be static to work properly.
DThe where clause is incorrect; it should be 'stock' >= 1.
Attempts:
2 left
💡 Hint
Check if the scope method returns the query builder.
🧠 Conceptual
expert
2:00remaining
How do global query scopes differ from local query scopes in Laravel?
Select the statement that correctly describes the difference between global and local query scopes.
AGlobal scopes automatically apply to all queries on the model, while local scopes must be explicitly called.
BLocal scopes apply to all queries automatically, but global scopes require manual invocation.
CGlobal scopes can only filter by one column, local scopes can filter by multiple columns.
DLocal scopes are defined in service providers, global scopes inside models.
Attempts:
2 left
💡 Hint
Think about when each scope type is applied during querying.