laravel scope example

Example 1: laravel scope relationship

class User extends Model {

    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    public function scopeWomen($query)
    {
        return $query->whereGender('W');
    }

}

Example 2: laravel scope

public function apply(Builder $builder, Model $model)
    {
        $builder->where('age', '>', 200);
    }

Example 3: laravel scope query

$model = App\Models\Flight::where('legs', '>', 100)
            ->firstOr(['id', 'legs'], function () {
                // ...
            });

Example 4: laravel scope relationship

$users = User::popular()->women()->orderBy('created_at')->get();

Tags:

Php Example