Learn how to use Laravel's 4.2 GlobalScopes
. More info softonsofa.com/laravel-how-to-define-and-use-eloquent-global-scopes
- clone the repo with
git clone https://github.com/jarektkaczyk/laravel4-global-scope-example.git
- run
composer install
in order to pull all the packages - migrate the db (sqlite = no setup required) with
php artisan migrate
- seed the db with
php artisan db:seed
- run the REPL with
php artisan tinker
and play with thePost
model:
[1] > Post::toSql();
// default behaviour - scope applied
// 'select * from "posts" where "posts"."published" = ?'
[2] > Post::withDrafts()->toSql();
// scope removed using static method on the trait
// 'select * from "posts"'
[3] > Post::query()->withDrafts()->toSql();
// scope removed using macro on the Eloquent\Builder
// 'select * from "posts"'
[4] >