हम जानते हैं कि Database Tables एक दुसरे से relate करती हैं , इसलिए इन Databases को RDBMS (Relational Database Management System) कहते हैं। जैसे - MySQL . हालाँकि हो सकता है कि एक single record सिर्फ single record से relate करे या कई सारे records से भी relate कर सकता है।


For Example - एक single user के एक से ज्यादा Addresses हो सकते हैं। या एक single post के कई सारे likes / dislikes , comments हो सकते हैं।

Laravel Types Of relationships

Laravel Eloquent Relationships technique इन सभी तरह के relatable tables को easily manage कराती है। different - different relations के लिए Laravel में different - different relations हैं , उनमे से कुछ इस प्रकार हैं।

  1. One To One
  2. One To Many
  3. Many To Many

Laravel Defining Relationships

Laravel Eloquent relationships , आपके Eloquent Model (placed इन app directory) में as a method define किया जाता है। और जब records fetch करते हैं तो उस method को call कर लिया जाता है।

Laravel One To One

one-to-one relationship , सबसे basic relationship है। माना हर user का एक single address है।

Important
हम ऐसा मान कर चलते हैं कि , सभी users के लिए Database Table - users है और Eloquent Model Name - User है। और इसी तरह user का address store करने के लिए एक Database Table - user_address और Eloquent Model Name - UserAddress है।

अब user_address table में user_id name के field में users Table की primary id save की है तो उसके लिए one-to-one relationship कुछ इस तरह से बनाते हैं।


अब User model में एक address() नाम से method बनाएंगे और उस method में है हम one-to-one relationship define करेंगे।

File : app/User.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function address()
    {
        return $this->hasOne('App\UserAddress',  'user_id',  'id');
    }
}

hasOne() method में तीन arguments pass किये गए हैं -

  1. first argument , related Eloquent Model का name है।
  2. second argument , related Eloquent Model में store की गयी current table की foreign id है।
  3. third argument , current table की local id है।

अब आप user के address को आप as a property access कर सकते हैं । जो आपको UserAddress Eloquent Model का complete collection return करता है।

$user_address = User::find(1)->address;

/*we can access it like this:*/
$user = User::find(1);
$user_addess = $user->address;

Laravel Defining The Inverse Of One To One Relationship

तो अभी हमने एक One To One relationship बनाया , जिससे हम user record से user address access कर सकते थे , अब UserAddress पर एक Inverse relationship बनाएंगे जिससे UserAddress record से हम user को access कर पायंगे।

File : app/UserAddress.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserAddress extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function user()
    {
        return $this->belongsTo('App\User',  'user_id',  'id');
    }
}

Inverse relationship के लिए हम belongsTo() method का use करते हैं। जो तीन arguments accept करता है।

  1. first argument , Model Eloquent जिससे current record belong(relate) करता है।
  2. second argument , foreign key होती है , जो की current table model में stored होती है।
  3. और third argument आपके parent Model की primary key होती है।

अब अगर किसी address के लिए हमें user find करना है तो उसे कुछ इस तरह से करेंगे।

$user = UserAddress::find(1)->user;

/*we can also access it like this:*/
$user_addess = UserAddress ::find(1);
$user = $user_addess->user;

Laravel One To Many

जब एक record कई सारे records से relate करता हो वहां पर On To Many relationships use करते हैं। Example के लिए single post कई सारे comments हो सकते हैं।


On To Many relationships define करने के लिए हम Eloquent Model में hasMany() method का use करते हैं , जो बाकी methods की तरह ही argumanets accept करता है।

File : app/Post.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment', 'post_id', 'id');
    }
}

एक बार relationship define हो जाने के बाद , आप किसी post से related सभी comments के collections को comments property से access कर सकते हैं।

$comments = App\Post::find(1)->comments;
foreach ($comments as $comment) {
    //
}

हालाँकि अगर आप चाहते हैं , कि सभी post records के साथ ही उस post के comments भी एक साथ आये तो आप with() method की help से कर सकते हैं।

$post_with_comments = App\Post::with('comments')->get();
foreach ($post_with_comments as $post) {
    /*get all comments for current post*/
    $comments = $post->comments;
    foreach ($comments as $comment) {
       //
    }
}

Laravel One To Many Inverse

अभी आपने One To Many Relationship का example देखा , जिसमे एक parent के सभी related child collections को access किया, अब One To Many Inverse Relationship देखेंगे जिसमे child collection से इसका parent record access हो सके।


One To Many Inverse Relationship के लिए भी हम belongsTo() method का use करते हैं।

File : app/Comment.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post', 'post_id', 'id');
    }
}

Inverse Relationship define करने के बाद आप , किसी comment की post को access करने के लिए simply post property से access कर सकते हैं।

$comment = App\Comment::find(1);
echo $comment->post->title;

/*we can also do it.*/
comments_with_post = App\Comment::with('post')->get();
foreach ($comments_with_post as $comment) {
    $comment->description; /* for comment description */
    $comment->post->title;  /* for comment post title */
}

Laravel Many To Many

Many To Many Relationship , One To One और One To Many Relationship से थोड़ी ज्यादा complex है। For Example - एक user के कई सारे Roles हैं और वो Roles कई सारे अलग users से भी associate हो सकते हैं।

Table Structure For Many To Many relationships

Many To Many Relationship के लिए Database table structure थोड़ा अलग होगा। इसके लिए तीन Tables होगी - users , roles , and role_user . role_user table में हम हर एक user के लिए user_id और role_id insert करेंगे।

users
    id - integer
    name - string

roles
    id - integer
    name - string

role_user
    user_id - integer
    role_id - integer

Laravel Model Structure For Many To Many Relationship

बाकी relationships की तरह ही Many To Many Relationship में भी एक method को Model में define किया जाता है , लेकिन यहां belongsToMany() method result return करता है।

File : app/User.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
    }
}

relationships define हो जाने के बाद roles property का use करके उस user के सभी roles collections access कर सकते हैं।

$user = App\User::find(1);
$roles = $user->roles;
foreach ($roles as $role) {
    //
}

/*we can use it like this*/
$users = App\User::with('roles')->get();
foreach ($users as $user) {
    $roles = $user->roles;
    foreach ($roles as $role) {
      // 
    }
}

Laravel Many To Many Inverse Relationship

Many To Many Inverse Relationship के लिए भी belongsToMany() method use किया जाता है , लेकिन दूसरे (Role) Eloquent Model में।

File : app/Role.php

CopyFullscreenClose Fullscreen
<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User', 'role_user', 'user_id', 'role_id');
    }
}

और फिर एक role से सभी associated users को कुछ इस तरह से get कर सकते हैं।

$role = App\Role::find(1);
foreach ($role->users as $user) {
    //
}

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers