<p> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174119929536701.jpg" class="lazy" alt="Learn to master Query Scopes in Laravel">
<lar> ?? ??????? ?? ? ?? ?? ???? ??? ?? ???? ???? ?? ???? ??? ???? ? ?? ????. ??? ?? ??? ?? ????? ???? ??? ??? ?? ????? ?? ??? </lar></p> ?? ??? ????? ???????. ?? ???? ???? ??? ??? ???? ?? ? ?? ??? ????? ?? ??? <p> ?? ??? ????? ???????. <code>where</code>
Laravel??? ?? ???? ???? ??? ??? ? ?? ???? ???? ??? ? ? ????. <code>where</code>
? ????? ?? ?? ??? ??? ?? ??? ?? ? ????. ??? ? ??? ??, ??? ?? ??? ??? ?? ? ???? ???? ??? ??????. </p>
<article>? ??? ?? ??? Laravel ?? ?????? ?? ???? ???? ??? ? ??????. <p>
<ery> ?? ??? ? ?????? </ery></p>
<p>
<ope> ?? ??? ???? ??? ??? ???? ?? ??? ?? ??? ?? ? ? ????. ??? ????? Laravel ??? ??? ?? </ope></p> ?????? ???? ???? ?????. <p>
<they> ??? ??? ??? ? ??? ???? ? ??? ?? ? ??? ??? ?? ?? ? ??? ?? ?? ??? ?? ??? ? ?? ?? ?? ? ????. </they></p>
<ang> ?? ??? ? ?? ???? ????
<h1>
<r> ?? ?? ?? - ??? ??? ???? ??? ???????. </r>
</h1>
<sc> ??? ?? ??? - ????? ??? ??? ??? ?? ? ?? ??? ?? ??? ?????. <hr>
<p>
<ever> Laravel? ?? ? "Soft Delete"??? ??? ?? ?? ??? ?????? ???? ? ????. Laravel? ?? ?? ??? ???? ???? <code>IlluminateDatabaseEloquentScope</code> ? </ever></p>? ?? ???? ?????. ?? ??? ?? ??? ???? ??? ?? ??? <p> ?? ??? ???? ???? ???? ????? ??? ?? ???? ???? ??????. </p>
<how> Laravel ???????? ?? ?? ??? ? ??? ?? ???? ???? ???? ??? ?? ?????. <p>
?? ?? ???
</p>
?? ?? ??? ?? ??? ???? ???? ?? ??? ???? ?? ? ??? ?? ??? ?? ? ? ????. <ul>
<are> ??? ????? ??? ?? ????? ????? ?? ? ???. ??? ???? ?? ? ??? ???? ???? ???? ?? ??? ???? ???? ? ???? ????. <li><that> ??? ??? ???? ??? ???? ????? ?????? ???? ??? ???? ?? ??? ?????? ? <p> ?? ??? ?????. ??? <code>AppModelsArticle</code>? ???? ??? ???? ?? ? ??? ?????. ??? ? ????? <code>published_at</code> ? ?? ??? ???? ???? ?? ??? ?????. <code>published_at</code>
<ished> ?? ? ??? ???? ???? ?? ??? ? ? ????.
<code>published_at</code>
??? ??? ???? ???? ?? ??? ??? ? ????.
<code>null</code>
<ery> ?? ??? ??? ???? ????. ??? ?? ???? ??? ?? ???? ????? ?? ? ???. ?? ??? ???? ?? ??? ????? ? ??? ?? ????? ?? ?? ???? ???? ????. ?? ??, ???? ??? </ery></ished></p> ?? <p>? ???? ?? ? ??? ???? ?? ? ? ??????. ?? ??? ???? ?????? ???? ??? ?? ? ? ??? ?? ??? ?????????. </p>
<qu> ??? ?? ???? ?? ??? ????. ??? <pre class="brush:php;toolbar:false"><code>use App\Models\Article;
$publishedPosts = Article::query()
->where('published_at', '<', now())
->get();</code></pre> ???? ?? ?? ??? ??? ??? ?????. <p>
<sc> ?? ?? ???? </sc></p>? ???? ???? ??? ???? ?? ???? ??? ???? ???? ?????. ?? ??, <pre class="brush:php;toolbar:false"><code>use App\Models\Article;
use Illuminate\Contracts\Database\Eloquent\Builder;
$unpublishedPosts = Article::query()
->where(function (Builder $query): void {
$query->whereNull('published_at')
->orWhere('published_at', '>', now());
})
->get();</code></pre>?? ???? ???? <p> ??? ?????. ? ???? <accept accept> ????? ???? ????? ???????. <code>>=</code>
<r> ??? ? ??? <code><</code> ??? ????? : <?? ??>
</p>
<in> ?? ??? ?? ??, <p> ?? ??? ?? ???? ? ?? ??? ???? ?????. ?? ??? ??? ??? ?? ??? ??? ? ????.
<code>AppModelsArticle</code>
<opinion> ???? ???? ??? ??? ?? ????? ? ??? ?? ?????. ??? ?? ?? ??? ?? ???? ??? ??????? ??? ??? ??? ? ? ??? ?????. </p>
<sc> ??? ?? ??? <p>
<code>scope</code>
<sc> ??? ?? ???? ?? ?? ??? ??? ??? ?????. ??? ???? ???? ???? ??? ??? ?? ??? ???? ?????. <code>scopePublished</code>
<earlier> ??? ????? Laravel? ?? "Soft Delete"??? <code>published</code> Global Query ??? ?????. ? ??? ??? ?? ??? ???? <code>IlluminateContractsDatabaseEloquentBuilder</code> ?? ??? ?????. ?? ??? ???? ? ??? ???? GitHub? ?? ??? ??????. <code>IlluminateContractsDatabaseEloquentBuilder</code>
?? ?? <sup> ??? ????? ?? ??? ??? ?? ????? ????? ?? ? ???. ???? ?? ?? ??? ? ? ??? ?? ? ?? ????. ??? ??? ?? ??? ? ? ????<pre class="brush:php;toolbar:false"><code>use App\Models\Article;
$publishedPosts = Article::query()
->where('published_at', '<', now())
->get();</code>
? ??? ??? <p> ?? ??? ???? ?? ?? ????. ?? ??? ???? ?? ??? ???? ?? ?? ?? ?? ?? ????? ??? ???? ???? ?? ??? ?? ??? ?? ??? ? ????. ??, ??? ?? ?? ????? ??? ????! <code>where</code>
?? ???? ?? ?? ?? ??? ???? ?? ? ??? ?? ??? ?? ? ????. </p>
<create>#?? ?? ??? ??? ??
<a> ?? ?? ??? ????? ?? ?? ??? ??? ???. <code>AppModelArticle</code>
<for>? ??? ?? ?, ??? ???? ?????. ?? ?? ??????? ???? ???? ???? ???? ?? ?? ??? ?? ?? ??? ??? ???? ??? ?? ??? ??? ? ????. ??? ????? ???? ???? ??? ?? ??? ??? ??? ? ????. </for></a></create></r></accept></p>
<run> ??? ?? ????? ?? ?? ??? ??? ???? : <h3>
</h3>
<created> ??? ??? ??? ???????. ? ??? ???? ? ?? ?? ? ??? ?? ???.
<p>
<code> ?? ?? ???? ?????? ???? ?? ?? ?????? ??? ???? ??? ? ? ????. ??? ?? ??? ?? ? ?? ??? ???? ?????. <code>team_id</code>
<ope> ??? ??? ??? ??? ? ????. ??? ??? ?? ???? ?? ??? ??? ?? ? ? ????. </ope></code></p>
<it> <p> ??? ?????. </p>
<global>#Apply Global Query Scope
<ways> ??? ??? ??? ???? ? ?? ??? ????. ? ?? ??? ???? <p> ??? ?????? ????.
</p>
<to to> ?? ??? <code>use App\Models\Article;
use Illuminate\Contracts\Database\Eloquent\Builder;
$unpublishedPosts = Article::query()
->where(function (Builder $query): void {
$query->whereNull('published_at')
->orWhere('published_at', '>', now());
})
->get();</code> ????? <p> ???? ???? ????.
<code>app/Models/Scopes/TeamScope.php</code>
<the> ? ?? ?? </the></p> ??? ?? ??? <pre class="brush:php;toolbar:false"><code>declare(strict_types=1);
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
final class Article extends Model
{
public function scopePublished(Builder $query): Builder
{
return $query->where('published_at', '<', now());
}
public function scopeNotPublished(Builder $query): Builder
{
return $query->where(function (Builder $query): Builder {
return $query->whereNull('published_at')
->orWhere('published_at', '>', now());
});
}
// ...
}</code></pre> ?? ??? ?????. <p>
<can> ??? ?? <code>IlluminateDatabaseEloquentScope</code> ?? ???? ?? ???? ?? ??? ? ? ??? ?????.
<code>apply</code>
<ume> ???? </ume></can></p> ? ? ?? ???? ???? ?? ??? ?? ?? SQL? ?????.
<p>
??? ???, ????? ! </p>
<ous> #Anonymous Global Query Scope <p>
<def> ??? ?? ??? ???? ???? ? ?? ??? ??? ??? ??? ???? ????. <code>AppModelsArticle</code>
<our> ??? ??? ??? ???? ?? ??? ??????? :
</our></def></p>
<code> ? ?? ???? <h3 id="????-????-???"> ???? ???? ??? </h3> ????? ??? ??? ??? ??????. <ts> ???? ? ?? ?? ??? ????? : <ul>
<the the> ??? ?? - ???? ?? ???? ??? ??? ???? ?? <li>? ?? ? ? ????.
<ain> ??? ?? ?? - ?? ??? ?? ? ??? ?????
</ain>
</li>
<methods> ?? ??? ?????, ??? ??? ?? ??? <li> ?? ??? ?????. </li>
? ??? ??? ??? ??? ??? ????? ??? ??? ???? ?? ? ????? ????. ??? ?? ??? ?? ?? ????? ?? ?? ?? ????. </methods></the>
</ul>
<global>#inignore Global Query Scope
<want> ??? ??? ??? ??? ?? ??? ???? ?? ??? ??? ?? ????. ?? ??, ??? ?? ??? ???? ?? ???? ?? ???? ??? ?? ?? ??? ?? ? ? ????. <p>
<the>? ?? ? ?? ?? ? ??? ???? ?? ??? ??? ? ????. <code>where('team_id', Auth::user()->team_id)</code>
<is> ? ?? ??? <code>AppModelsArticle</code>???. ? ??? ?? ??? ???? ??? ??? ?? ??? ???? ??? ? ????.
</is></the></p>
<you> ?? ??? ??? ??? ?? ? ????? ?? ??? <p> ???? ??? ? ???? : .
</p>
<above> ?? ???, ??? ?? ? ?? ??? ??? ??? ??? ?????.
<you> ?? ?? ??? ?? ? ????? <p> ???? ??? ? ????.
</p>
??? ?? ?? ??? ?? ??? ??? ?? ???? ???? ????? ?? ???? ?? ?????. ??? ???? ?????? ??? ???? ??? ?? ??? ???? ????. <p>
?? ?? <sup>,? ??? ???? ??? ? ??? ?? ?? ? ?????? ???? ?? ? ???.
</sup></p>
<qu> ?? ???? <p> ??? <code>withoutGlobalScopes</code>? ??? ?? ??? ?? ???? ??? ???? ????. ??? ?????? ???? ?? ??? ???? ??????????. </p>
<ery> ??? ?? ?? ??
<pre class="brush:php;toolbar:false"><code>use App\Models\Article;
$publishedPosts = Article::query()
->where('published_at', '<', now())
->get();</code></pre>
<have> ?? ?? ???? ??? ???? ??? ????? ???? ???? ??? ?? ?????. <p>
<ways> ??? ??? ????? ???? ?? ??? ??? ??? ??? ?? ??? ?? ???? ??? ??? ?? ?? ? ????. ?? ??, ??? ?? ? ?? ?? ??? ???? ??? ? ????. ?? ????? ?? ?????? ??? ????? ? ?? ?? ??? ???? ??? ?? ????. <code>withoutGlobalScopes</code>
<i> ?????, ?? ? ??? ???? ??? ??? ?? ??? ???? ???? ??? ??? ???? ??? ??? ? ????. </i></ways></p>
<with> ?? ?? <pre class="brush:php;toolbar:false"><code>use App\Models\Article;
use Illuminate\Contracts\Database\Eloquent\Builder;
$unpublishedPosts = Article::query()
->where(function (Builder $query): void {
$query->whereNull('published_at')
->orWhere('published_at', '>', now());
})
->get();</code></pre> ? <p> ???? ???? ?? ???? ???????. ??? ? ?? ?? ??? (? ???? ???)? ???????.</p>
<ul>
??? ?? <li> ??? ?? ? ?? ? ?????. <code>published</code>
??? ?? ??? ???? ?? ?? ? ?????. </li>
<li>
<at>? ?????? ???? ??? ?? ????? : <code>notPublished</code>
</at>
</li>
<in> ?? ??? ???? ? ? ????. ?? </in>
</ul> ???? ?? ???? ????. ??? ? ?? ?? ? ??, ??? ???? ?? ??? ??? ?? ? ??? ??????. <p>
<a> ?? ?? ?? ? ?? ? ???? ?? </a></p> ??? ???? ??? ()? ????. ??? ?? ? ????? ??? ???? ??? ()? ????. <pre class="brush:php;toolbar:false"><code>use App\Models\Article;
$publishedPosts = Article::query()
->where('published_at', '<', now())
->get();</code></pre>
??????? ?? ??? ???? ?? ??? ???? ??? ??? ? ????. <p>
<controll> ????? ??? ??
<code>setUp</code>
??? ????? ??? ??? ????? ? ?? ??? ????? ?? ? ?????? ?? ??? ????? ????. ???? ?? ???? ???? ??? ??? ?? ??? ???? ??? ?? ? ? ??? ??? ???? ???? ?? ?????? ????? ?????? ????. ?? ??, ???? ???? ??? </controll></p> ??? ???? ?? ?? ? ????. <p>
<err> ??? ??? ??? ???? ???? ???? ??? ? ??? ???? ???? ???? ???? ?? ? ? ????. <code>only_published_articles_are_returned</code>
<the> ?? ??? ??? ????????? ?? ?? ??? ???? ???? ??? ?? ???? ???????. ??? ?? ?? ??? ???? ??? ??? ?? ? ???.
<code>published</code>
<that> ??? <code>only_not_published_articles_are_returned</code> ??? ??? ? ????? ?????. <code>notPublished</code>
<ert> ??? ??? ?? ?? ?? ? ????? ?????. ??? ???? ??? ?? ?? ? ????
</ert></that></the></err></p>
<test> ?? ????? ??? ? ?? ??? ????. ?? ?? Team One? ?? ???? ????. ??? ? 1??? 3 ?? ??? ? 2? ?? ??? ??????. ?? ?? ??? ????? ??? ???? ???? ??? ?????. ???? ??? Team One? ??? ?? 3 ?? ??????? ?? ID? ???? ?? ?? ? ????? ?????. <p>
?? ???? ???? ???? ??? ?? ??? ???? ??? ?????. </p>
<h1> ??
</h1>
? ????? ?? ?? ??? ??? ?? ??? ?? ?????. ??? ?? ??? ???, ??? ???? ??, ???? ???? ??? ?????. <hr>
<now> ?? Laravel ???????? ?? ???? ???? ??? ? ??? ????. <p></p></now></test></with></have></ery></qu></you></above></you></want></global></ts></code></ous></to></ways></global></it></created></run></qu></that></li></are>
</ul></how></sc></ang></article>
? ??? Laravel? ?? ???? ????? ?? ?????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!