How To Create a One To Many Relationship in Laravel 9?
Teams applying “How To Create a One To Many Relationship in Laravel 9?” in a production project can use step rate compensation structure to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi friends,
Today I explain how to create a one to many relationship in laravel 9. so now in this tutorial i will show you laravel 9 one to many eloquent relationship. When one table refers to many rows in another table that is called a one-to-many relationship. A one-to-many relationship is a very basic type of database relationship in laravel 9.
Using one to many relationship, you can perform crud (create, read, update, delete) operation with the eloquent model from the database table in laravel 9.
So let's start following example:
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Add 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:
php artisan make:migration create_posts_table
database\migrations\2014_10_12_000000_create_posts_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
comments table migration:
php artisan make:migration create_comments_table
database\migrations\2014_10_12_000000_create_comments_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained('posts');
$table->string("comment");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
};
Step 3: Add Models
Here, we will create Post and Comment table model. we will also use "hasMany()" and "belongsTo()" for relationship of both model.
php artisan make:model Post
app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
/**
* Get the comments for the blog post.
*
* Syntax: return $this->hasMany(Comment::class, 'foreign_key', 'local_key');
*
* Example: return $this->hasMany(Comment::class, 'post_id', 'id');
*
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
}
Comment Model:
php artisan make:model Comment
app/Models/Comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
/**
* Get the post that owns the comment.
*
* Syntax: return $this->belongsTo(Post::class, 'foreign_key', 'owner_key');
*
* Example: return $this->belongsTo(Post::class, 'post_id', 'id');
*
*/
public function post()
{
return $this->belongsTo(Post::class);
}
}
Step 4: Retrieve Records:
php artisan make:controller PostController
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$comments = Post::find(1)->comments;
dd($comments);
}
}
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$post = Comment::find(1)->post;
dd($post);
}
}
Step 5: Add Records:
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Comment;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$post = Post::find(1);
$comment = new Comment;
$comment->comment = "Hi ItSolutionStuff.com";
$post = $post->comments()->save($comment);
}
}
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Comment;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$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]);
}
}
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\Comment;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$comment = Comment::find(1);
$post = Post::find(2);
$comment->post()->associate($post)->save();
}
}
I hope it will help you..
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial