Laravel Eloquent Relationships

Websolutionstuff | Mar-23-2021 | Categories : Laravel

In this example we will see Laravel Eloquent relationships, laravel provides many relationship like laravel hasmany, laravel belongsto, wherehas laravel, many to many relationship laravel, eloquent relationships, one to many relationship laravel, one to one relationship laravel, here we will see all relationship with example.

There are 3 types of relationships in laravel:

  • One to one
  • One to many
  • Many to many

 

One to One

The one to one relationship is basic in laravel. This type of relationship means that a model of type A may only be linked to one model of type B, and vice versa. For example, a relationship between a User model and a Passport model would be a one to one relationship. A user can only have one passport, and a passport only belongs to one user.

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function passport() {
        return $this->hasOne(App\Passport::class);
    }
}

 

Otherside Passport model we will define the inverse of the relationship. We let the Passport model know that it belongs to a User.

 


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Passport extends Model
{
    public function user() {
        return $this->belongsTo(App\User::class);
    }
}

 

 

One to many

One to Many relationship means that one model of type A may be linked to multiple models of type B. But the model of type B belongs to only one model of type A.

Example, a relationship between a User model and an Invoice model would be a one to many relationship. A user can have multiple invoices, but an invoice only belongs to one user.

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function invoices() {
        return $this->hasMany(App\Invoice::class);
    }
}

 

All we have to do now is let the Invoice model know that it belongs to a User model. here we define the inverse of the one to many relationship.

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    public function user() {
        return $this->belongsTo(App\User::class);
    }
}

 

Many to many

Many to many relationship means that one model of type A may be linked to multiple models of type B, and vice versa.

For example, a relationship between an Invoice model and a Product model would be a many to many relationship. An invoice can have multiple products and a product can be on multiple invoices.

 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    public function products() {
        return $this->belongsToMany(App\Product::class);
    }
}

 

And you can define the inverse of this relationship like below:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function invoices() {
        return $this->belongsToMany(App\Invoice::class);
    }
}

Many to many relationships are slightly more difficult to implement because they require a junction table in the database. You can create this junction table in Laravel by creating a migration file.

 

Recommended Post
Featured Post
Laravel 9 Image Upload Example
Laravel 9 Image Upload Example

In this tutorial, I will explain the laravel 9 image upload example. image or file upload is the most common task in web...

Read More

Feb-28-2022

How To Add Bootstrap 5 Modal Popup In Laravel 9
How To Add Bootstrap 5 Modal P...

In this article, we will see how to add bootstrap 5 modal popup in laravel 9. We will learn how to use the bootstrap 5 m...

Read More

Nov-02-2022

Remove/Hide Columns While Export Data In Datatable
Remove/Hide Columns While Expo...

In this article, we will see remove or hide columns while exporting data in datatable. When we are using jquery dat...

Read More

Aug-24-2020

Laravel 9 Livewire Dependent Dropdown
Laravel 9 Livewire Dependent D...

In this article, we will see the laravel 9 livewire dependent dropdown. Here, we will learn how to create a dependent dr...

Read More

Nov-29-2022