One to Many relationship uses when we have to combine one table with multiple tables. For example, a post may have multiple comments, a class may have many students, etc. So in this tutorial, we will cover one to many eloquent relationship and how to use them in laravel.
To make the one to many eloquent relationship in laravel, you have to define the method in your eloquent Model classes. Then that method you can use in your controller to get, insert, update or delete the records from database tables.
There are more other types of eloquent relationships and each type has a different role of use. All are easy to use but the polymorphic relationship is a bit complicated. But you don’t need to worry about that I will explain in easy ways one by one laravel tutorial.
Let’s get started!!!
Laravel One To Many Eloquent Relationship Example
A one-to-many relationship is very easy and we will use
hasMany() methods to implement. In this relationship, one model has ‘local_key‘ and the other models have ‘foreign_key‘.
Eloquent relationships determine the ‘foreign_key‘ name based on the Model name. But if you want to override the ‘local_key‘ and ‘foreign_key‘ then you can do it by passing the additional parameters in the hasMany() method.
So let’s get started with creating a laravel application, migrations, models and then make a one-to-many eloquent relationship.
Create a Laravel Application
First thing you need to create a new laravel application if you don’t have one. So use the following command to install the laravel application.
Note: Make sure you have composer installed on your machine. If it’s not installed then see the full tutorial of laravel installation in more detail.
Create Migrations of Tables
After installation, you need to create migration files for the database table. Let’s create migrations for Posts and Comments tables.
Run the following commands in your terminal to create the migration files. It will create two files in the /database/migrations folder.
Now you have to add the columns in these files as you want. For this tutorial, we are taking the following migration example files for ‘posts‘ and ‘comments‘ tables.
After making the changes in migration files, now you have to run the following command to create the database tables using these migration files structures.
Create Models ‘Post’ and ‘Comment’
Now let’s create models of Posts and Comments where we will define the eloquent methods respectively. So run the following commands to make models.
In the Post model, we will create a ‘comments’ method using the hasMany() method and link with a Post model class as a parameter.
Now you have to add some dummy data in both tables that will be relative to another table. You can add manual data in the tables Or you can make it with the laravel database seeder feature. It will create dummy data for you.
Create ‘MainController’ and Call Methods
Now you have to create a controller to call those methods to get the results accordingly. Use the following commands to create a controller.
The above command will create the controller file like the following. Now create a
get_comments() functions as following.
We use the dynamic property as a reference for those methods that we have defined in the models. Now you can get the records from database tables, create the new records, update and delete the records. You can use the tinker CLI to test the application.
Create Routes to Get the Records
To create the routes, go to the /routes folder and open the web.php file. In this file, you can create routes for your application. Add the following two routes in this file.
And to call these API routes you can enter the following path in your browser to get the results.
So in this tutorial, you have learned about one to many eloquent relationship in laravel. And you see how to define the relationship between one parent model to other one or more child models.
As you see we used the
belongsTo() methods in models to make a relationship with eloquent models and then used those methods as dynamic property in the controller to get the results.
If you still have any queries please do let me know in the comment section, I will respond to you as soon as possible and help you with that.