Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: states eloquent scopes return affects query building #10301

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions eloquent.md
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ User::withoutGlobalScopes([

Local scopes allow you to define common sets of query constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, add the `Scope` attribute to an Eloquent method.

Scopes should always return the same query builder instance or `void`:
Scopes should always return the same query builder instance or `void`. If the scope return is void you need to start your query with `Model::query()`:

```php
<?php
Expand Down Expand Up @@ -1522,21 +1522,21 @@ Once the scope has been defined, you may call the scope methods when querying th
```php
use App\Models\User;

$users = User::popular()->active()->orderBy('created_at')->get();
$users = User::query()->popular()->active()->orderBy('created_at')->get();
```

Combining multiple Eloquent model scopes via an `or` query operator may require the use of closures to achieve the correct [logical grouping](/docs/{{version}}/queries#logical-grouping):

```php
$users = User::popular()->orWhere(function (Builder $query) {
$users = User::query()->popular()->orWhere(function (Builder $query) {
$query->active();
})->get();
```

However, since this can be cumbersome, Laravel provides a "higher order" `orWhere` method that allows you to fluently chain scopes together without the use of closures:

```php
$users = User::popular()->orWhere->active()->get();
$users = User::query()->popular()->orWhere->active()->get();
```

<a name="dynamic-scopes"></a>
Expand Down Expand Up @@ -1569,7 +1569,7 @@ class User extends Model
Once the expected arguments have been added to your scope method's signature, you may pass the arguments when calling the scope:

```php
$users = User::ofType('admin')->get();
$users = User::query()->ofType('admin')->get();
```

<a name="pending-attributes"></a>
Expand Down Expand Up @@ -1604,7 +1604,7 @@ class Post extends Model
The `withAttributes` method will add `where` conditions to the query using the given attributes, and it will also add the given attributes to any models created via the scope:

```php
$draft = Post::draft()->create(['title' => 'In Progress']);
$draft = Post::query()->draft()->create(['title' => 'In Progress']);

$draft->hidden; // true
```
Expand Down