Laravel 6 self join table in Eloquent

Today, We want to share with you Laravel 6 self join table in Eloquent.In this post we will show you Laravel 6.0 Eloquent ORM where join table, hear for How to Join Multiple Table in Laravel 6.2 we will give you demo and example for implement.In this post, we will learn about Laravel 6 inner join with multiple conditions example using Query Builder with an example.

Laravel 6 self join table in Eloquent

There are the Following The simple About laravel eloquent self relation Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to join two models in laravel, so the PHP Laravel select with join subquery example is used for this example is following below.

Self Join in Eloquent – How To Call

$member1 = Member::find(1);
$submember = Member::where('id', '<>', $member1->id)->
    where('teamid', $member1->teamid)->
    get();

using DB

$res = DB::table('members as mkd')->
    select('mtdes.name')->
    join('members AS mtdes', 'mtdes.teamid', '=', 'mkd.teamid')->
    where('mkd.id', 1)->
    where('mtdes.id', '<>', 'mkd.id')->
    get();

Laravel 6 SQL Self Join Example

Example 2: Self Join in Laravel 6 Eloquent

Member.php

public function submember()
{
    return $this->hasMany(Member::class, 'parent_id');
}

public function parent()
{
    return $this->belongsTo(Member::class, 'parent_id');
}

Member.php

$member->submember; // sub-members collection

$member->parent; // parent instance

This is the Best Solution Step By Step (Working Fine) so far in Laravel 6.

laravel – Self Join in Eloquent

In Laravel’s Eloquent ORM, you can perform self joins by creating a relationship between a model and itself. A self join is useful when you have a table that contains a hierarchical structure or a tree-like structure.

Here’s an example of how to set up a self join in Laravel:

Suppose we have a Category model, and the categories table has the following structure:

id
name
parent_id

The parent_id field contains the ID of the parent category. If the parent_id is null, then the category is a top-level category.

To create a self join relationship, you need to define a relationship method in the Category model that refers to itself. Here’s an example:

class Category extends Model
{
    protected $fillable = ['name', 'parent_id'];

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }
}

In this example, we have defined two relationship methods: parent() and children(). The parent() method defines a belongsTo relationship between the Category model and itself. The children() method defines a hasMany relationship between the Category model and itself.

Now, you can use these relationship methods to fetch data from the categories table. Here are some examples:

To fetch all top-level categories:

$categories = Category::whereNull('parent_id')->get();

To fetch all categories with their parent:

$categories = Category::with('parent')->get();

To fetch all categories with their children:

$categories = Category::with('children')->get();

To fetch all categories with their parent and children:

$categories = Category::with(['parent', 'children'])->get();

I hope this helps you set up self join relationships in Laravel’s Eloquent ORM.

Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about How to setup Eloquent Models for self-joins,.
I would like to have feedback on my infinityknow.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