Laravel 8 One to Many relationships
In this article I will tell you how can we can do One to Many relationships in Laravel 8.
A one to many relationships is used to define relationships where a single model is a parent to one or more child models.
For example, a blog post may have an infinite number of comments.
Like all other Eloquent relationships, one to many relationships are defined by defining a method on your Eloquent model.
If you want to learn more about relationships in Laravel you can visit Laravel’s official website from here.
To create a one-to-many relationship we will create two tables one is Post and another is a comment.
For one to many relationship we will follow below steps -:
- Create Migration and Model
- Write Model code
- Retrive Records
- Create Records
Step – 1 Create Migration and Model
To create Migration and Model together run below command.
First we will create Model and Migration for Post Table after that we will create for Comment table.
php artisan make:model Post -m
Now we will create Model and Migration For Comment Table using single command.
php artisan make:model Comment -m
Now go to app/database/migration here you can find your post migration file, open that file and write the below code
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string("name"); $table->timestamps();
Now go to app/database/migration here you can find your comment migration file, open that file and write the below code
Schema::create('comments', function (Blueprint $table) { $table->id(); $table->integer('post_id')->unsigned(); $table->string("comment"); $table->timestamps(); $table->bigInteger('post_id')->unsigned()->index(); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); });
Step -2 Write Model Code
Now go to app/Models and find for Post.php file and write the below code.
<?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); } }
Now go app/Models and find for Comment.php file and write the below code.
<?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); } }
Step -3 Retrieve Record
Now we will Retrieve Record using Laravel Eloquent, use the below code to retrieve Record.
$post = Post::find(1); $comments = $post->comments; dd($comments);
$comment = Comment::find(1); $post = $comment->post; dd($post);
Step – 4 Create Record
Now we will create Record using Laravel eloquent.
$post = Post::find(1); $comment = new Comment; $comment->comment = "Hi codehunger.in"; $post = $post->comments()->save($comment);
$post = Post::find(1); $comment1 = new Comment; $comment1->comment = "Hi codehunger.in Comment 1"; $comment2 = new Comment; $comment2->comment = "Hi codehunger.in Comment 2"; $post = $post->comments()->saveMany([$comment1, $comment2]);
post = Post::find(2); $comment->post()->associate($post)->save();
I hope you like the above article, If you have any confusion, Please give your comment.