@@ -62,44 +62,45 @@ public function boot()
6262
6363Add a select using a Closure. Each ` Post ` model, published or not, will have an ` is_published ` attribute.
6464``` php
65- $posts = Post::addScopeAsSelect('is_published', function ($query) {
65+ Post::addScopeAsSelect('is_published', function ($query) {
6666 $query->published();
6767})->get();
6868```
6969
7070The example above can be shortened by using a string, where the second argument is the name of the scope:
7171``` php
72- $posts = Post::addScopeAsSelect('is_published', 'published')->get();
72+ Post::addScopeAsSelect('is_published', 'published')->get();
7373```
7474
7575You can use an array to call multiple scopes:
7676``` php
77- $posts = Post::addScopeAsSelect('is_popular_and_published', ['popular', 'published'])->get();
77+ Post::addScopeAsSelect('is_popular_and_published', ['popular', 'published'])->get();
7878```
7979
8080Use an associative array to call dynamic scopes:
8181``` php
82- $posts = Post::addScopeAsSelect('is_announcement', ['ofType' => 'announcement'])->get();
82+ Post::addScopeAsSelect('is_announcement', ['ofType' => 'announcement'])->get();
8383```
8484
8585If your dynamic scopes require multiple arguments, you can use an associative array:
8686``` php
87- $posts = Post::addScopeAsSelect('is_announcement', ['publishedBetween' => [2010, 2020]])->get();
87+ Post::addScopeAsSelect('is_announcement', ['publishedBetween' => [2010, 2020]])->get();
8888```
8989
9090You can also mix dynamic and non-dynmaic scopes:
9191``` php
92- $posts = Post::addScopeAsSelect('is_published_announcement', [
92+ Post::addScopeAsSelect('is_published_announcement', [
9393 'published',
9494 'ofType' => 'announcement'
9595])->get();
9696```
9797
9898The method has an optional third argument that flips the result.
9999``` php
100- $posts = Post::addScopeAsSelect('is_not_announcement', ['ofType' => 'announcement'], false)->get();
100+ Post::addScopeAsSelect('is_not_announcement', ['ofType' => 'announcement'], false)->get();
101101```
102102
103+
103104## Usage
104105
105106Imagine you have a ` Post ` Eloquent model with a query scope.
0 commit comments