Home >Database >Mysql Tutorial >Laravel Eloquent ORM in Bangla Part-Query Scopes)
Laravel Eloquent's Query Scopes feature allows recursive queries to be stored in methods to make code reusable. This keeps the code nice and understandable, especially when the same type of query is needed over and over again. Query Scopes define common queries as methods, which can then be used throughout the model.
Global Scopes are always attached to the model. This scope works automatically when you use the model, you don't have to write it every time. It is usually used to check login status, active records etc.
1. Create global scope class:
To create a Global Scope in Laravel, a class that implements the Scope
interface is used.
<code class="language-php">use Illuminate\Database\Eloquent\Scope; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; class ActiveScope implements Scope { public function apply(Builder $builder, Model $model) { $builder->where('active', 1); // সক্রিয় ফিল্টার } }</code>
2. Attach global scope to model:
This should be added to the booted()
method of your model.
<code class="language-php">use App\Models\Post; use App\Scopes\ActiveScope; class Post extends Model { protected static function booted() { static::addGlobalScope(new ActiveScope); } }</code>
Now using Post::all()
will automatically apply the active = 1
filter.
3. Temporarily omitting global scope:
<code class="language-php">use Illuminate\Database\Eloquent\Scope; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; class ActiveScope implements Scope { public function apply(Builder $builder, Model $model) { $builder->where('active', 1); // সক্রিয় ফিল্টার } }</code>
This will drop the global scope temporarily and return all posts, except the active
filter.
Local Scopes acts as a specific query scope inside the model and only applies when you call it explicitly. It is not automatically applied during every query like global scope.
Create a local scope by adding a public method to the model, whose first argument Builder
is input.
<code class="language-php">use App\Models\Post; use App\Scopes\ActiveScope; class Post extends Model { protected static function booted() { static::addGlobalScope(new ActiveScope); } }</code>
<code class="language-php">$posts = Post::withoutGlobalScope(ActiveScope::class)->get();</code>
Parameters can be passed to the local scope.
<code class="language-php">use App\Models\Post; class Post extends Model { // লোকাল স্কোপ public function scopeActive($query) { return $query->where('active', 1); } public function scopeDraft($query) { return $query->where('status', 'draft'); } }</code>
Scope can now be used with the status
parameter:
<code class="language-php">// সক্রিয় পোস্ট পেতে: $posts = Post::active()->get(); // ড্রাফট পোস্ট পেতে: $draftPosts = Post::draft()->get(); // চেইন করে ব্যবহার: $activeDraftPosts = Post::active()->draft()->get();</code>
'published' is the parameter.
Dynamic Scope is a type of local scope, where the scope name can be called dynamically. Laravel allows the use of scopeName()
type names.
<code class="language-php">class Post extends Model { // লোকাল স্কোপ public function scopeStatus($query, $status) { return $query->where('status', $status); } }</code>
scope can now be called dynamically:
<code class="language-php">$posts = Post::status('published')->get();</code>
This works just like the scopePublished()
method.
Multiple scopes can be chained. For example, the status
and active
scopes can be used together:
<code class="language-php">use Illuminate\Database\Eloquent\Scope; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; class ActiveScope implements Scope { public function apply(Builder $builder, Model $model) { $builder->where('active', 1); // সক্রিয় ফিল্টার } }</code>
This will apply both active
and status('published')
.
Eloquent's Local Scopes are chainable, meaning multiple scopes can be used simultaneously.
<code class="language-php">use App\Models\Post; use App\Scopes\ActiveScope; class Post extends Model { protected static function booted() { static::addGlobalScope(new ActiveScope); } }</code>
There are three scopes and one orderBy
chained.
The above is the detailed content of Laravel Eloquent ORM in Bangla Part-Query Scopes). For more information, please follow other related articles on the PHP Chinese website!