Home » Laravel Nested Relationships in Eloquent Example

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.

Read Also:  Laravel Eloquent Parent Child Relationship

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:

Read Also:  PHP Laravel 5.6 Delete Multiple Rows using Checkbox

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.

Read Also:  PHP codeigniter form validation Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Merge Multiple PHP JSON Array Merge into one array
  2. Read Also:  call javascript function from html
  3. Read Also:  Laravel 5.8 Dropzone Multiple Image Upload
  4. Read Also:  How to Create Model In Laravel 6 Example?
  5. Read Also:  PHP Dynamic PDF Files Tutorial using fpdf
  6. Read Also:  PHP Download Images From URL Using cURL Example
  7. Read Also:  Get Innerheight-Outerheight-Scrollheight Clientheight using JavaScript
  8. Read Also:  Laravel Eloquent with Collection Contains
  9. Read Also:  Laravel Collection Contains Example
  10. Read Also:  Merge Multiple PHP JSON Array Merge into one array

Leave a Comment

Your email address will not be published. Required fields are marked *