| 131 | // ── Eloquent Closure Parameter Inference ──────────────────────────────── |
| 132 | |
| 133 | public function eloquentClosure(): void |
| 134 | { |
| 135 | // Eloquent chunk — $orders inferred as Collection |
| 136 | BlogAuthor::where('active', true)->chunk(100, function ($orders) { |
| 137 | $count = $orders->count(); // resolves to Eloquent Collection |
| 138 | echo $count; |
| 139 | }); |
| 140 | |
| 141 | // Explicit bare type hint inherits inferred generic args for foreach |
| 142 | BlogAuthor::where('active', true)->chunk(100, function (Collection $authors) { |
| 143 | foreach ($authors as $author) { |
| 144 | $author->posts(); // resolves to BlogAuthor via Collection<int, BlogAuthor> |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | // Eloquent whereHas — $query inferred as Builder<BlogPost> (the related model) |
| 149 | BlogAuthor::whereHas('posts', function ($query) { |
| 150 | $query->where('published', true); // resolves to Builder<BlogPost> |
| 151 | }); |
| 152 | |
| 153 | // Dot-notation relation chain |
| 154 | BlogPost::whereHas('author', function ($q) { |
| 155 | $q->where('active', true); // resolves to Builder<BlogAuthor> |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | // ── Laravel Config & Env Navigation ───────────────────────────────────── |