Laravel Scopes Generator


September 23rd, 2019


The laravel-make-scope package by Samson Endale adds a make:scope command to the Artisan console. Here’s an example of creating a scope class with this command:

1php artisan make:scope ClientScope

And the generated PHP class:

1namespace App\Scopes;
3use Illuminate\Database\Eloquent\Builder;
4use Illuminate\Database\Eloquent\Model;
5use Illuminate\Database\Eloquent\Scope;
7class ClientScope implements Scope
9 /**
10 * Apply the scope to a given Eloquent query builder.
11 *
12 * @param \Illuminate\Database\Eloquent\Builder $builder
13 * @param \Illuminate\Database\Eloquent\Model $model
14 * @return void
15 */
16 public function apply(Builder $builder, Model $model)
17 {
18 // $builder->where('age', '>', 200);
19 }

You can read more about query scopes from the Laravel documentation. To learn more about this package, get full installation instructions, and view the source code on GitHub at SamAsEnd/laravel-make-scope.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.