Laravel Nested Eager Loading on Polymorphic Relationships

Laravel Nested Eager Loading on Polymorphic Relationships

Sometimes you need to eager load different relationships depending on the type of model on a polymorphic relationship.

For example: you have two type of users seller and buyer, the…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Muath Alsowadi

Laravel Nested Eager Loading on Polymorphic Relationships

Sometimes you need to eager load different relationships depending on the type of model on a polymorphic relationship.

tip

For example: you have two type of users seller and buyer, the eager load relationship for seller is the product and the eager load relationship for buyer is the order.

  • First setup the Models.
class Seller extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}
class Buyer extends Model
{
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}
class Profile extends Model
{
    public function user()
    {
        return $this->morphTo();
    }
}
  • Then load the relations like the following:
Profile:with('user', function (MorphTo $morphTo) {
    // Eager load the products for seller.
    // Eager load the orders for buyer 👇
    $morphTo->morphWith([
        Seller::class => ['products'],
        Buyer::class => ['orders'],
    ]);
})->get();


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Muath Alsowadi


Print Share Comment Cite Upload Translate Updates
APA

Muath Alsowadi | Sciencx (2023-02-13T21:40:53+00:00) Laravel Nested Eager Loading on Polymorphic Relationships. Retrieved from https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/

MLA
" » Laravel Nested Eager Loading on Polymorphic Relationships." Muath Alsowadi | Sciencx - Monday February 13, 2023, https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/
HARVARD
Muath Alsowadi | Sciencx Monday February 13, 2023 » Laravel Nested Eager Loading on Polymorphic Relationships., viewed ,<https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/>
VANCOUVER
Muath Alsowadi | Sciencx - » Laravel Nested Eager Loading on Polymorphic Relationships. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/
CHICAGO
" » Laravel Nested Eager Loading on Polymorphic Relationships." Muath Alsowadi | Sciencx - Accessed . https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/
IEEE
" » Laravel Nested Eager Loading on Polymorphic Relationships." Muath Alsowadi | Sciencx [Online]. Available: https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/. [Accessed: ]
rf:citation
» Laravel Nested Eager Loading on Polymorphic Relationships | Muath Alsowadi | Sciencx | https://www.scien.cx/2023/02/13/laravel-nested-eager-loading-on-polymorphic-relationships/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.