| 68 | // ── Eloquent Query Builder ────────────────────────────────────────────── |
| 69 | |
| 70 | public function eloquentQuery(): void |
| 71 | { |
| 72 | // Builder-as-static forwarding |
| 73 | BlogAuthor::where('active', true); |
| 74 | BlogAuthor::where('active', 1)->get(); // → Collection<BlogAuthor> |
| 75 | BlogAuthor::where('active', 1)->first(); // → BlogAuthor|null |
| 76 | BlogAuthor::orderBy('name')->limit(10)->get(); |
| 77 | BlogAuthor::whereIn('id', [1, 2])->groupBy('genre')->get(); |
| 78 | BlogAuthor::where('active', 1)->first()->profile->getBio(); |
| 79 | |
| 80 | // Model @method tags available on Builder (e.g. SoftDeletes withTrashed) |
| 81 | BlogAuthor::where('active', 1)->withTrashed()->first(); |
| 82 | BlogAuthor::groupBy('genre')->onlyTrashed()->get(); |
| 83 | |
| 84 | // Scope methods — instance and static |
| 85 | $author = new BlogAuthor(); |
| 86 | $author->active(); |
| 87 | $author->ofGenre('fiction'); |
| 88 | BlogAuthor::active(); |
| 89 | BlogAuthor::ofGenre('fiction'); |
| 90 | |
| 91 | // Scopes on Builder instances (convention and #[Scope] attribute) |
| 92 | BlogAuthor::where('active', 1)->active()->ofGenre('sci-fi')->get(); |
| 93 | Bakery::where('open', true)->freshlyBaked()->get(); |
| 94 | $query = BlogAuthor::where('genre', 'fiction'); |
| 95 | $query->active(); |
| 96 | $query->orderBy('name')->get(); |
| 97 | |
| 98 | // where{PropertyName}() dynamic methods (from $fillable, $casts, etc.) |
| 99 | Bakery::whereFlour('whole wheat'); // from $fillable |
| 100 | Bakery::whereApricot(true); // from $casts |
| 101 | Bakery::whereDefrostedAt('2024-01-01'); // from $dates |
| 102 | Bakery::whereCroissant('almond'); // from $attributes |
| 103 | Bakery::whereKitchenId(42); // from $guarded |
| 104 | Bakery::whereOvenCode('X9'); // from $hidden |
| 105 | Bakery::whereFlour('rye')->whereApricot(true)->get(); |
| 106 | Bakery::where('open', true)->whereFlour('spelt')->freshlyBaked()->first(); |
| 107 | |
| 108 | // Conditionable when()/unless() chain continuation |
| 109 | BlogAuthor::where('active', 1)->when(true, fn($q) => $q)->get(); |
| 110 | BlogAuthor::where('active', 1)->unless(false, fn($q) => $q)->first(); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | // ── Custom Eloquent Collections ───────────────────────────────────────── |