googleads
Laravel Eloquent Relationships Guide | Hire Laravel Developers
preloder
Laravel Web Development

A Guide To Laravel Eloquent Relationships – Part 1

In our previous blogs, we had discussed more about Laravel and it’s features, also the future advancements and so on. But today we’re getting bit closer to Laravel and will jump into the world of Laravel Eloquent relationships. As we all aware about the Laravel eloquent relationships which is currently introduced and available from laravel 5.0 onwards. To be precise, here we will have foreign keys while creating an application and with that, each table will be connected to the other table where the Eloquent remains easy for connecting each tables. On the go, will explore each relationships and it’s fondness towards facilitating relationships around tables.

  • One to one relationships

  • One to many relationships

  • Many to many relationships

 

Why Eloquent Relationships?

Here we have 2 tables named as “students” and “marks”so to join this tables we need to apply following query,

 

Now you can note that the above query is too long to proceed, so when we try to connect more tables then it would be too tough and complicated to handle big query thats why we’re moving to Laravel eloquent relationships.

Model Query Using Relationships

 

The above example is a simple example of eloquent relationships and we can reduce the first query into a simple one.

One To One Relationships

Here we are creating 2 tables as “Users” and “Phones” so now we can see one to one relationships of this tables using hasone() and belongsto().

For this, we need to create the table using migrations

Related: Laravel API – How to Build REST API with Laravel 5.5 / Laravel 5.6

 

How To Create Migrations

 

After creating migrations, we need to create a model for each table. Hope you already know that usually the laravel table name will be ending with “s”(Plural) and model name will be without ‘s’ so you can also maintain the same standards.

User Model

 

 

One To Many Relationships

Here we will use hasMany() and belongsTo() for one to many relationships. Now we are creating two tables, posts and comments, and we will be having a foreign key towards posts table.

Also Read: Generate Charts And Graphs Using Laravel

 

Migrations For Posts And Comments Table

 

Let’s create Post Model and Comment Model

Post Model

 

Now we can create records through Comments Model,

 

To create records

 

 

To get records

 

Many To Many Relationships

Many to many is little bit different and complicated than the above two.

In this example, I will create users, roles, and role, users_tables, here each table will be connected each other using the foreign keys.

Using belongsToMany() we will use see a demo of Many to many relationship

Create Migrations

 

How To Create Models In Many To Many Relationships

 

User Model

 

 

Role Model

 

 

UserRole Model

 

 

To Create Records

 

 

To Retrieve Records

 

Hence laravel Eloquent is more powerful and helps to build relationships easily compared to native query. With eloquent relationship we can easily connect the tables each other and we also have three more relationships in laravel but we will see more about it in our next blog as part 2 so stay tuned with us!

  • Has Many Through Relationship
  • One To Many Polymorphic
  • Many To Many Polymorphic

Similarly you can learn more interesting blogs on latest technologies, never miss out anything from  our largest blog portal where you can get continuous blog updates & latest posts about all the latest technologies which would be perfect for your 15 minutes tea break! In case if you’re a newbie then don’t forget to subscribe us to get the latest updates from diverse technologies. What else guys hit the  subscribe link  and go crazy over learning. For more inquires reach us via info@agiratech.com

The following two tabs change content below.

Harikrishnan R

Around 3 years of experience he scored intense knowledge on PHP, Laravel, Symfony, Angular & ensuring to learn the new & best methods to bring excellence in building applications. Also he has some aspiring dream to do Doctorate and the strange reason behind this will definitely leave a smile on your face! Yeah because he just loves writing his name as Dr. Harikrishnan

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call