Laravel One to Many Eloquent Relationship

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello Dev’s
Today i show you One to Many relationship. It will use when one table associated with multiple tables. A good example is, a post has multiple comments. One to many eloquent relationship in laravel5.*,laravel 6, laravel 7 or laravel 8 application.

So now in this tutorial i will show you Laravel One to Many Eloquent Relationship , you can also understand how to create migration with a foreign key schema for one to many relationships. By create table, create records; also get all data, delete, update and everything related to one to many relationships.

Now in this example, I will create a “posts” table and also a “comments” table. Both tables are connected with each other. Now i will show how to make one to many relationships with each other by using the laravel Eloquent Model. We will first create model then database migration, also retrieve records. And then how to create records too.

In One to Many Relationship we will use “hasMany()” and “belongsTo()” for relation.

Create Migrations:

Now just we have to create a migration of “posts” and aslo for “comments” table. We will also need to add foreign key with posts table. So let’s start and create like as below:

posts table migration:

Schema::create('posts', function (Blueprint $table) {

    $table->increments('id');

    $table->string("name");

    $table->timestamps();

});

Read Also : How to Autocomplete Textbox in Laravel 8 with Ajax?

comments table migration:

Schema::create('comments', function (Blueprint $table) {

    $table->increments('id');

    $table->integer('post_id')->unsigned();

    $table->string("comment");

    $table->timestamps();

    $table->foreign('post_id')->references('id')->on('posts')

        ->onDelete('cascade');

});

Here, now we will create Post and Comment model for table. We will also use “hasMany()” and “belongsTo()” for relationship of both model.

Post Model:

<?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(Comment::class);
    }
}

Comment Model:

<?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(Post::class);
    }
}

Create Records:

$post = Post::find(1);
 
$comment = new Comment;
$comment->comment = "Hi Codingspoint.com";
 
$post = $post->comments()->save($comment);
$post = Post::find(1);
 
$comment1 = new Comment;
$comment1->comment = "Hi ItSolutionStuff.com Comment 1";
 
$comment2 = new Comment;
$comment2->comment = "Hi ItSolutionStuff.com Comment 2";
 
$post = $post->comments()->saveMany([$comment1, $comment2]);
$comment = Comment::find(1);
$post = Post::find(2);
 
$comment->post()->associate($post)->save();
Find result
$post = Post::find(1);
 
$comments = $post->comments;
 
dd($comments)
$comment = Comment::find(1);
 
$post = $comment->post;
 
dd($post);

Read Also : How to use Union query with Laravel Eloquent?

Thanks For read ,Hope it will help you………….

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →