Laravel One to Many Eloquent Relationship Tutorial
One to Many relationship will use when one table associated with multiple tables. For example, a post may have multiple comments. one to many eloquent relationship in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 example.
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->id();
$table->string("name");
$table->timestamps();
});
comments table migration:
Schema::create('comments', function (Blueprint $table) {
$table->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\Models;
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\Models;
use Illuminate\Database\Eloquent\Model;
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]);
$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:
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Where Query Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Order By Relation Column Example
- Laravel Eloquent Concat Two Columns Example
- Laravel Order By Relationship Sum Column Example
- Laravel Relationship Eager Loading with Condition Example
- Laravel Relationship Eager Loading with Count Example
- Laravel Relationship Where Condition Example
- How to use Union Query with Laravel Eloquent?
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to One Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel One to Many Polymorphic Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial