Laravel Nested Relationships in Eloquent Example

Today, We want to share with you Laravel Nested Relationships in Eloquent Example.In this post we will show you Laravel Deeply nested relationships, hear for Get a nested relationship in Laravel we will give you demo and example for implement.In this post, we will learn about Eloquent nested relations with where clause with an example.

Laravel Nested Relationships in Eloquent Example

There are the Following The simple About Laravel Nested Relationships in Eloquent Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Retrieving nested relationships using Laravel, so the Nested relationships when parent is a many-to-many relationship for this example is following below.

Laravel Eloquent Relationships Tutorial Example From Scratch

This is the Laravel Simple SQL Relationships query that works!

SELECT member.id, 
       member.firstname, 
       member.lastname, 
       team_scores.score 
FROM   teams 
       JOIN countries 
         ON countries.id = teams.country_id 
       JOIN vanues 
         ON countries.id = vanues.country_id 
       JOIN member 
         ON vanues.id = member.vanue_id 
       JOIN team_scores 
         ON team_scores.member_id = member.id 
WHERE  team_scores.team_id = 1 
GROUP  BY member.id 

These are my Eloquent relations

Team Model

class Team extends Eloquent
{
    protected $table = 'teams';

    public function country()
    {
        return $this->belongsTo('Country');
    }
}

Country Model

class Country extends Eloquent
{
    protected $table = 'countries';

    public function vanues()
    {
        return $this->hasMany('Vanue');
    }

    public function team()
    {
        return $this->hasMany('Team');
    }
}

Vanue Model

class Vanue extends Eloquent {

    protected $table = 'vanues';

    public function member()
    {
        return $this->hasMany('Member');
    }

    public function country()
    {
        return $this->belongsTo('Country');
    }
}

Member Model

class Member extends Eloquent
{
    protected $table = 'member';

    public function vanue()
    {
        return $this->belongsTo('Vanue');
    }

    public function teamscore()
    {
        return $this->belongsToMany('Team', 'team_scores', 'member_id', 'team_id')
            ->withPivot('score')
            ->withTimestamps();
    }
}

PHP Laravel nested relationships Get Data

return Team::with('country.vanues.member')->get();

If you only want to select simple all the fields from the member table, use this:

return Team::with(['country.vanues.member' => function ($query) {
    $query->select('id', '...');
}])->get();
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Nested Relationships in Eloquent Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment