In this tutorial you will learn about the Laravel One to Many Relationship Example and its application with practical example.
In this Laravel one to many relationship example tutorial I’ll show you how to implement one to many relationship in laravel eloquent model. You will also learn how to use create, and retrieve records from database tables using one to many relationship.
- Laravel One to Many Relationship Example
- Laravel Eloquent One to Many Relationship Example
- Define One To Many Relationship
- Defining Inverse One To Many Relationship in Laravel
- Retrieving Data In One to many relationship
- Inserting Data In One to Many relationship
- Filtering Data In one to many relationship
- Deleting Data In One to Many relationship
Laravel One to Many Relationship Example
In this step by step tutorial I’ll guide you through to create many to many relationship and learn how to use this relationship. In this example we will use one to many relationship, and perform crud (create, read, update, delete) operation with the eloquent model from the database table in laravel.
Laravel Eloquent One to Many Relationship Example
Let suppose we have tables name posts and authors. Using laravel migration, you can create both tables with the following fields:
Author table migration:
1 2 3 4 5 6 7 8 9 |
Schema::create('authors', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); |
Post table migration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->integer('author_id')->unsigned(); $table->timestamps(); $table->foreign('author_id')->references('id')->on('authors') ->onDelete('cascade'); }); |
Define One To Many Relationship
Now we will define One To Many Relationship for the model we have created. In One to many relationships means a relationship where one single model can owns the number of the other model. As per the example model we created, a single author can have written many post articles. Let’s take an example of one to many relationships.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Author extends Model { ... public function post() { return $this->hasMany('App\Post'); } } |
Now, we can access the collection of post articles by using the post() method as.
1 2 3 4 5 |
$posts = App\Author::find(10)->post()->get(); foreach ($posts as $post) { //do something } |
Defining Inverse One To Many Relationship in Laravel
As of now we can access all the post articles of an author, now suppose if we want to allow a post model to access its Author model or access author details by using the post model. The inverse relationship of both One to One and One to Many works the same way. The inverse of the one to many relationship can be defined using the belongsTo method. Thus we can define in example mode as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function author() { return $this->belongsTo('App\Author'); } } |
Now we can get the author details by using posts in one to many relationship as following:
1 |
$post->author->name; |
Retrieving Data In One to many relationship
In one to many relationship you can retrieve data from model as following:
You can get all posts of a particular author:
1 2 3 4 5 |
$posts = Author::find(10)->post()->get(); foreach ($posts as $post) { //do something } |
Similarly, you can retrieve the inverse related model.
1 2 3 4 5 |
$posts = Post::find(10)->author()->get(); foreach ($posts as $post) { //do something } |
Inserting Data In One to Many relationship
To Insert data in one to many relationship between two models, we can first create a child object and then save it using the parent object as following:
1 2 3 4 5 6 7 8 |
$author = Author::find(1); $post= new Post(); $post->title = 'iPhone X'; $post->description = 'Some description'; // Saving related model $author->post()->save($post); |
Filtering Data In one to many relationship
If you want to filter the related records and want them to sort when retrieving. You can chain the methods on your retrieving query.
1 2 3 4 |
$author = Author::find(1); $post = $author->post() ->orderBy('name', 'asc') ->get(); |
Deleting Data In One to Many relationship
Deleting of records in one to many relationship is performed same as we have created them. To delete all posts for a particular author use below code example.
1 2 |
$author = Author::find(1); $author->post()->delete(); |