Laravel

Get Nested Relationships From Morph Relationship – Laravel 8+

Share your learning

Have you used the morph relationship of laravel, then you might encounter this issue. Getting nested relationships from morph relations is a little bit difficult. Let’s go through it one by one.

Example 1: With Nested morph relation

I was working on a service selling platform, so here is the scenario. We need to find the transactions of a particular user with a transaction item that can be anything like Project Revision, Bonus Tips, or project itself.

$user->transactions()->with(['beneficiary',  'invoice',  'item' =>
                                    function (MorphTo $morphTo) {
                                        $morphTo->morphWith([
                                            Revision::class => [‘project’, ‘project.owner'],
                                            BonusTip::class => ['item' => function (MorphTo $morphTo2) {
                                                $morphTo2->morphWith([
                                                    Project::class => ['owner']
                                                ]);
                                            }],
                                            Project::class => ['owner']
                                        ]);
                                    }])

So, Illuminate\Database\Eloquent\Relations\MorphTo is a useful solution for this problem, because it allows us to get nested relationships from morph relation in laravel.

Example 2: Load Nested morph relation

If you already have a collection and now want to get nested relationships from morph relationships then you can also use the following solution.

$activities = $transactions
    ->loadMorph(‘item’, [
         Revision::class => [‘project’, ‘project.owner'],
         BonusTip::class => ['item' => function (MorphTo $morphTo) {
                              $morphTo->morphWith([
                                   Project::class => ['owner']
                              ]);
                          }],
         Project::class => ['owner']
    ]);

Cheers,

You will also like ajax file upload tutorial here.

We did it.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago