Laravel One to Many Eloquent Relationship Tutorial

Laravel One to Many Eloquent Relationship Tutorial

In this post we will give you information about Laravel One to Many Eloquent Relationship Tutorial. Hear we will give you detail about Laravel One to Many Eloquent Relationship TutorialAnd how to use it also give you demo for it if it is necessary.

One to Many relationship will use when one table associated with multiple tables. For example, a post may have multiple comments.

So in this tutorial, you can understand how to create migration with a foreign key schema for one to many relationships, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationships.

In this example, I will create a “posts” table and “comments” table. both tables are connected with each other. now we will create one to many relationships with each other by using the laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.

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

Create Migrations:

Now we have to create migration of “posts” and “comments” table. we will also add foreign key with posts table. so let’s create like as below:

posts table migration:

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

$table->increments('id');

$table->string("name");

$table->timestamps();

});

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');

});

Create Models:

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

Post Model:

<?php


namespace App;


use IlluminateDatabaseEloquentModel;


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 IlluminateDatabaseEloquentModel;


class Comment extends Model

{

/**

* Get the post that owns the comment.

*/

public function post()

{

return $this->belongsTo(Post::class);

}

}


Retrieve Records:

$post = Post::find(1);

$comments = $post->comments;

dd($comments);

$comment = Comment::find(1);

$post = $comment->post;

dd($post);

Create Records:

$post = Post::find(1);

$comment = new Comment;

$comment->comment = "Hi ItSolutionStuff.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]);

Also see:Laravel One to One Eloquent Relationship Tutorial

$comment = Comment::find(1);

$post = Post::find(2);

$comment->post()->associate($post)->save();

I hope you understand of one to many relationship…

*** Click On it and Read in Details of RelationShip types:

Hope this code and post will helped you for implement Laravel One to Many Eloquent Relationship Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

32  +    =  33

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US