Skip to content

Commit ed3c051

Browse files
committed
Adds docs
1 parent 8e5d937 commit ed3c051

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

readme.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,148 @@ ShippedOrder::becoming(function ($shippedOrder) {
236236
});
237237
```
238238

239+
## Eager Loading Child Models
240+
241+
To help with eager-loading relationships on child models, Parental provides a set of helpers that you may use in your queries. For the examples, we'll use the following models:
242+
243+
```php
244+
class Room extends Model
245+
{
246+
public function messages(): HasMany
247+
{
248+
return $this->hasMany(Message::class);
249+
}
250+
}
251+
252+
class Message extends Model
253+
{
254+
use HasChildren;
255+
256+
protected $fillable = ['type', 'content'];
257+
258+
protected $childTypes = [
259+
'text' => TextMessage::class,
260+
'image' => ImageMessage::class,
261+
];
262+
}
263+
264+
class TextMessage extends Message
265+
{
266+
use HasParent;
267+
268+
public function mentions(): HasMany
269+
{
270+
return $this->hasMany(User::class);
271+
}
272+
}
273+
274+
class ImageMessage extends Message
275+
{
276+
use HasParent;
277+
278+
public function attachments(): HasMany
279+
{
280+
return $this->hasMany(Attachment::class);
281+
}
282+
}
283+
```
284+
285+
### Eager Loading From Model Instance
286+
287+
You may eager-load relationships of different models from a parent model instance using the `loadChildren` method:
288+
289+
```php
290+
$message = Message::first();
291+
292+
$message->loadChildren([
293+
TextMessage::class => ['mentions'],
294+
ImageMessage::class => ['attachments'],
295+
]);
296+
```
297+
298+
This will ensure that, if `$message` is an instance of `TextMessage`, the `mentions` relationship will be eager-loaded. If it's an instance of `ImageMessage`, the `attachments` relationship will be eager-loaded.
299+
300+
Alternatively, you may eager-load the relationship counts using the `loadChildrenCount` method:
301+
302+
```php
303+
$message = Message::first();
304+
305+
$message->loadChildrenCount([
306+
TextMessage::class => ['mentions'],
307+
ImageMessage::class => ['attachments'],
308+
]);
309+
```
310+
311+
This will ensure that, if `$message` is an instance of `TextMessage`, the `mentions_count` attribute will be filled. If it's an instance of `ImageMessage`, the `attachments_count` attribute will be filled.
312+
313+
### Eager Loading From Eloquent Collection
314+
315+
You may eager-load relationships from an Eloquent Collection using the `loadChildren` method:
316+
317+
```php
318+
$messages = Message::all();
319+
320+
$messages->loadChildren([
321+
TextMessage::class => ['mentions'],
322+
ImageMessage::class => ['attachments'],
323+
]);
324+
```
325+
326+
This will ensure that the appropriate relationships are eager-loaded for each child model in the collection based on its type.
327+
328+
Alternatively, you may eager-load the relationship counts using the `loadChildrenCount` method:
329+
330+
```php
331+
$messages = Message::all();
332+
333+
$messages->loadChildrenCount([
334+
TextMessage::class => ['mentions'],
335+
ImageMessage::class => ['attachments'],
336+
]);
337+
```
338+
339+
This will ensure the `mentions_count` attribute will be filled for instances of the `TextMessage` model, and the `attachments_count` attribute will be filled for instances of the `ImageMessage` model.
340+
341+
### Eager Loading From Query and Relationship
342+
343+
You may eager-load relationships directly from a query or relationship using the `childrenWith` method:
344+
345+
```php
346+
// From a query...
347+
$messages = Message::query()->childrenWith([
348+
TextMessage::class => ['mentions'],
349+
ImageMessage::class => ['attachments'],
350+
])->get();
351+
352+
// From a relationship...
353+
$room = Room::first();
354+
$messages = $room->messages()->childrenWith([
355+
TextMessage::class => ['mentions'],
356+
ImageMessage::class => ['attachments'],
357+
])->get();
358+
```
359+
360+
This will ensure that the appropriate relationships are eager-loaded for each child model in the result set based on its type.
361+
362+
Alternatively, you may eager-load the relationship counts using the `childrenWithCount` method:
363+
364+
```php
365+
// From a query...
366+
$messages = Message::query()->childrenWithCount([
367+
TextMessage::class => ['mentions'],
368+
ImageMessage::class => ['attachments'],
369+
])->get();
370+
371+
// From a relationship...
372+
$room = Room::first();
373+
$messages = $room->messages()->childrenWithCount([
374+
TextMessage::class => ['mentions'],
375+
ImageMessage::class => ['attachments'],
376+
])->get();
377+
```
378+
379+
This will ensure the `mentions_count` attribute is filled on instances of the `TextMessage` model, and the `attachments_count` attribute is filled on instances of the `ImageMessage` model.
380+
239381
## Laravel Nova Support
240382

241383
If you want to use share parent Nova resources with child models, you may register the following provider at the end of the boot method of your NovaServiceProvider:

0 commit comments

Comments
 (0)