Eloquent پیشرفتهتر
Scope، Eager Loading و ابزارهای کاربردی.
Query Scope
PHP
// در مدل Post
public function scopePublished($query)
{
return $query->where('published', true);
}
// استفاده:
Post::published()->latest()->get();Eager Loading
PHP
// جلوگیری از N+1
$posts = Post::with('user', 'tags')->get();بدون with، برای هر پست یک کوئری جدا برای user زده میشود. با with تعداد کوئریها ثابت میماند.