Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6|Last
Lessons:-Eloquent ORM

11.2 Relationships
Relationships are beautiful. No I haven’t gone all soppy. I mean relationships between tables. In
Eloquent they are beautiful. None of that ‘JOIN’ rubbish! We can define ‘one-to-one’, ‘one-tomany’
and ‘many-to-many’ just by adding some simple methods to our Eloquent Models. Let’s
jump right in. You will learn more with a code snippet in front of you.
On To One
Jumping right in..
1 <?php
2
3 class User extends Eloquent
4 {
5 public function invite()
6 {
7 return $this->has_one('Invite');
8 }
9 }Let’s go ahead and assume that we have made an invites table and an Invite model to contain
our sexy party invites.
We have created a new public method called invite(), which will return $this->has_-
one('Invite') to find a users invite via a has one relationship.
Now we can retrieve a user’s (or party guest) invite with a clear expressive syntax. Let’s take a
peek:
1 <?php
2
3 $invite = User::find(1)->invite()->first();
Again we are using first() because we only want one result. You see by adding ->invite()->
to the chain, which is of course our relationship method name, we retrieve the Invite object
that is related to our user.
This relationship will execute two queries :
1 SELECT * FROM "users" WHERE "id" = 1;
2 SELECT * FROM "invites" WHERE "user_id" = 1;
From the query you will see that Eloquent is looking for user_id automatically as the foreign
key. So if we want to use this relationship, we will need to create a user_id integer field with
our migration, in the Invites table. What if we want to call our foreign key something else? No
problem! We simply pass a second parameter to the has_one() method (or other relationship
methods) to specify the new field name. For example..
1 <?php
2
3 class User extends Eloquent
4 {
5 public function invite()
6 {
7 return $this->has_one('Invite', 'the_invite_id');
8 }
9 }
What about the inverse of this relationship? What if we have an Invite, but we want to know
who it belongs to? This is where the belongs_to() method comes in handy. Let’s take a look at
the model.
1 <?php
2
3 class Invite extends Eloquent
4 {5 public function user()
6 {
7 return $this->belongs_to('User');
8 }
9 }
A similar syntax, but using belongs_to() instead to point out that the foreign key exists in this
table.
Now we can use..
1 <?php
2
3 $user = Invite::find(1)->user()->first();
Easy!
One To Many
What if we want to return many related items? Well as you may have guessed from the header
there’s a method for that. Let’s take a look. ( I say that a lot don’t I? Maybe it can be my
catchphrase. )
1 <?php
2
3 Class User extends Eloquent
4 {
5 public function hats()
6 {
7 return $this->has_many('Hat');
8 }
9 }
Again, you will see that we are passing the object name in string format with capitalisation to
the has_many() method.
In this example the hats table will need a user_id foreign key. We can then use..
1 <?php
2
3 $hats = User::find(1)->hats()->get();
Note that we could also use a dynamic property with the same name to retrieve the same results
with a shorter syntax. For example :1 <?php
2
3 $hats = User::find(1)->hats;
Nicer still!
As before, you can pass another foreign key as a second parameter to use that instead. Let’s
move on!
Many To Many
Here things will get a little more complicated, but don’t worry.. I am here to get you through
this. Take my hand Wendy. Let’s imagine that we have two Eloquent models, User and Task.
A user may have many tasks, and a task may have many users. For this type of relationship we
will need a third table.
This table is known by many names. A pivot table, a lookup table, an intermediate table, or el
pivote grande.
It’s simply a table with two integer fields user_id and task_id, that links the two tables together.
This forms a many to many relationship.
The table is named after both related tables in a plural form, in an alphabetical order. Sounds
complicated but it looks simple, check it out:
tasks_users
Great, now that we have our table, let’s form the relationship:
1 <?php
2
3 class User extends Eloquent
4 {
5 public function tasks()
6 {
7 return $this->has_many_and_belongs_to('Task');
8 }
9 }
again, similar syntax, with a slightly longer method name.
This time we can pass an optional second parameter to specify a different pivot table name.
Simple.

 

 

 

 
 
 

Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)