Home >Database >Mysql Tutorial >Laravel Eloquent ORM in Bangla Part-Query Scopes)

Laravel Eloquent ORM in Bangla Part-Query Scopes)

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-16 20:03:11868browse

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.

1. Global Scopes

Explanation:

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.

Creating the global scope:

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.

2. Local Scopes

Explanation:

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.

Creating local 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>

Using local scope:

<code class="language-php">$posts = Post::withoutGlobalScope(ActiveScope::class)->get();</code>

Adding parameter to local scope:

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.

3. Dynamic Scopes

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.

4. Multiple Scopes

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').

5. Chainable scope

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn