Professional Documents
Culture Documents
Mastering Laravel Eloquent Orm The Eloquent Journey Part 2
Mastering Laravel Eloquent Orm The Eloquent Journey Part 2
to
One To One
One To Many
Many To Many
One To One
id user_id address
1 1 20 Markson St.
$table->unsignedInteger('user_id')
$table->foriegnKey('user_id')->references('id')->on('users')->onDelete('cascad
adding the above to our profile table migration, laravel now uses
this to create our relationship between our models.
we can now define our relationship between our models. since the
foreign key is on the Profile model, we can now use the eloquent
hasOne() method to let eloquent bind our User model to the
profile model that a user in our users' table has a profile by
/**
*Returns an Eloquent relationship
**/
public function profile()
{
return $this->hasOne(Profile::class);
}
id custom_user_id address
1 1 20 Markson St.
return $this->hasOne(Profile::class,
'custom_user_id');
Eloquent also expect that on our user table the primary key is id,
but in cases we have a different primary key other that id,
user_id name
1 Samfield Hawb
return $this->hasOne(Profile::class,
'custom_user_id','user_id');
/**
*Returns an Eloquent Relationship
*/
public function user()
{
return $this->belongsTo(User::class);
}
Also here eloquent will try to match user_id on the profiles table
with an id on the users' table, and in situation you have a different
column name for our foreign key on our profile table,
id custom_user_id address
1 1 20 Markson St.
return $this->belongsTo(User::class,
'custom_user_id');
Eloquent also expect that on our user table the primary key is id,
but in cases we have a different primary key other than id,
user_id name
1 Samfield Hawb
return $this->belongsTo(User::class,
'custom_user_id','user_id');
With this set up, we can now use this relationship to perform a
query between this two models
$user = \App\User::find(1);
$user->profile;//the profile can now be gotten
by
One To Many
return
$this->hasMany(Post::class,'custom_user_id');
likewise if we have a diffent primary key for the parent model User
from the id column which Eloquent expect, we can also pass in a
third parameter to the hasMany() method
return
$this->hasMany(Post::class,'custom_user_id','user_id')
here eloquent will still try to match user_id on the posts table with
an id on the users' table, in situation you have a different column
name for our foreign key on our posts table, we can tell Eloquent by
providing the second parameter to our belongsTo() method
return $this->belongsTo(User::class,
'custom_user_id');
Eloquent still expect that on our user table the primary key is id,
but in cases we had a define primary key name other that id, we
can pass a third paramater to the belongsTo() method;
return $this->belongsTo(User::class,
'custom_user_id','user_id');
with this set up now, we can now make use of eloquent to query
data between the models. Lets get all the posts made by a user
with id of 1 from the database;
//lets print out the title for all the post made
foreach ($user->posts as $post)
{
echo $post->title;
}
we have seen how easy eloquent has made retrieving the post
here, with this relationship, we can create a post that binds to a
particular user;
$user = \App\User::find(1);
$user->posts()->create([
//we supply all keys representing the column
name and the value in
// 'key' => 'value'
]);
$post = \App\Post::find(1);
echo $post->user->name;
Many To Many
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
//foriegn key
$table->foriegnKey('user_id')->references('id')->on('users')->onDelete('cascad
$table->foriegnKey('role_id')->references('id')->on('roles')->onDelete('cascad
here, eloquent uses these two columns to fetch our data when
needed.
user_id role_id
1 1
1 4
1 3
2 4
We can see how this user with id 1 has 3 roles with ids 1, 4,
3.
Now on our User model we can now define our relationship using
the belongsToMany() method provided for use by Eloquent
Model class
Eloquent seeing this relationship will try to determine the table for
we created above by joining the two related models in alphabetical
other separated by an underscore role_user. In situations we
declared a name other than this, we can pass a second parameter
to our belongsToMany method to tell Eloquent where to find the
relationship.
return
$this->belongsToMany(Role::class,'custom_role_user');
Also, if we had custom keys for the table other than the expected
value, we can pass the third parameter as a foreign key for the
model we are defining the relationship and a fourth parameter for
the foreign key to the relating model
return
$this->belongsToMany(Role::class,'custom_role_user','custom_user_id','custom_r
we can then define the inverse of the relationship on our role model
too, which will enable use query all users that is tied to a particular
role.
With the relationships define, we can then fetch our data from the
database using eloquent ORM;
$user = \App\User::find(1);
foreach ($user->roles as $role)
{
echo $role->name
}
$role = \App\Role::find(1);
foreach($role->users as $user)
{
echo $user->name;
}
$user = \App\User::find(1);
$user->roles()->attach(1); //assigned role with id
1 to the user
also roles can be remove from users using the eloquent detach()
method;
$user = \App\User::find(1);
$user->roles()->detach(1); //remove role with id 1
to the user
$user->roles()->detach([2,3,4]); //remove roles
with ids 2,3,4 to the user
Conclusion