Laravel one to one eloquent relationship tutorial example

Laravel one to one eloquent relationship tutorial example

In this tutorial, today we discuss laravel one to one Eloquent relationship. Eloquent ORM means Object-relational Mapping and laravel provides a beautiful ActiveRecord structure. so we can easy to interact with the application database. let’s start about one to one eloquent relationship.

Laravel one to one relationship is based on the relation between two tables. so we need one table of the primary key and another table of reference key. For example, we have two tables: Students and Standards. both tables are connected as a one to one relationship with each other using the primary key and reference key.

Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

Create Table using migration

Now, We need to create a migration. so we will below command using create the students and standards table migration.

After complete migration. we need below changes in the database/migrations/create_students_table and database/migrations/create_standars_table file.



Run the below command. after the changes above file.

Create Model

Here below command help through we will create the Student and Standard model. we will also use “hasOne()” for a student model and “belongsTo()” for a standard model.



Route and Controller

We have to need put below student resource route in routes/web.

Here below command help to create the student controller.


Please follow and like us: