Home > Laravel > Laravel One to One Eloquent Relationship

Laravel One to One Eloquent Relationship

Laravel comes with a great feature called eloquent. It is an object-relational mapper (ORM) that makes it easy for you to interact with the database.

Using eloquent, each database has tables and each table has a separate Model file in your backend and you can control from there and interact with tables. So using the eloquent Model you can easily make a CRUD operation, which means you can insert, update, delete data from tables with very little code and in an easy way.

To make the eloquent relationship, 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 tutorial.

Today, we will deep dive into laravel’s One-To-One eloquent relationship. We will cover this step-by-step guide from starting to create an application to making an eloquent relationship with Modal.

Let’s get started!!!

Laravel One To One Eloquent Relationship Example

A one-to-one relationship is a very easy and basic type of database relationship. In this relationship, one model has ‘local_key‘ and another model has ‘foreign_key‘.

laravel one to one

Eloquent relationships determine the ‘foreign_key‘ name based on the Model name. But if you have a different name specified in the table then you can pass the third and fourth parameters in the method.

To better understand, let’s see with an example. Suppose you have a Customer model and Phone model. To make the one-to-one relationship in these two models, you have to define the ‘phone’ method in the Model class. Method name you can take accordingly.

And in this ‘phone’ method you have to call the hasOne() method to get the result. We will see this in the following example.

So let’s get started with creating a laravel application, migrations, models and then make an 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 Customer and Phone tables.

Run the following commands in your terminal to create the migration files. It will create two files in /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.

Customers migration file
Phones migration file

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 ‘Customer’ and ‘Phone’

Now let’s create models of Customer and Phone where we will define the eloquent methods respectively. So run the following commands to make models.

In the Customer model we will create a ‘phone‘ method using hasOne() method with associted a Phone model class as a parameter.

And in the Phone model we will create a ‘customer‘ method using belongsTo() method with specifying the Customer model class. Using this, it’s easy to identify that this phone belongs to a specific customer.

Customer Model
Phone Model

Now you have to add some dummy data in both tables that will be relative to another table. You can add manually data in the tables Or you can make with laravel database seeder feature. It will create dummy data for you.

Create ‘MainController’ and Call Methods

Now we will create any controller to call those methods ‘phone‘ and ‘customer‘. For this tutorial, I’m going to create ‘MainController‘ using the following command. You can create accordingly as you want.

After creating the controller, let’s call that two methods in this controller by using the Model class. See the following example as a reference.

You can see the above methods, we defiend the get_phone() and get_customer() method. You can further use where condition, orderBy condition, etc in laravel. Ok, now we can retireve the related data using the eloquent’s dynamic property.

The dynamic property allows you to access the relationship methods as if they were properties defined in the model class. In the above methods, you can see, we use the ‘phone’ and ‘customer’ methods as property with model class.

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.

Or you can create the new routes to read, create, update or delete the records.

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.

Now you can open the following two URLs in your browser one by one to get the data from database tables. Make sure your application is running.


Create Records

In the above example, we have initiated the Customer and Phone class to assign the values that we want to store in tables and then use the save() function to create the record.

Read Records

The first line of code will return the customer detail by phone id using the find() function and the second will return the phone detail by customer id.

Update Records

The above example will update the name in customer table and phone in phone table using the push() function.

Delete Records

You don’t need to delete the records from both tables one by one. You can delete records from both database tables at once together using the delete() function.

Conclusion

So in this tutorial, you learned about how to create laravel one-to-one eloquent relationship by creating the new laravel application, migration, models, and controller. From defining the methods in Models using the hasOne() and belongsTo() method to use these methods in controller with dynamic property to get the records from database tables.

You also learned how to create, read, update and delete a new record using the one-to-one eloquent relationship in laravel. If you have any queries please let me know in the comment section, I’ll help you with that.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment