
Defining Relationship:
- One to One relationship
- One to Many relationships
- Many to One relationship
- Many to Many Relationship
One to One Relationship
So, in the User.php model add the code to get permission to access the posts table by the user.
/* * One to one relationship method to get users single post
*/
public function post(){
return $this->hasOne('App\Post');
}
before that add an extra column to the 2020_08_11_053014_create_posts_table migration to refer the user identity. Then looks like
public function up(){
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->integer('user_id'); // new column added here
$table->string('body');
$table->boolean('is_admin')->default(0);
$table->timestamps();
});
}
Now again migrate, by running this commandphp artisan migrate:freshInsert some data to the posts table and users table. then,
In web.php file create a route to fetch a single record.
Route::get('/user/{id}/post',function ($id){
return User::find($id)->post;
});
Find the owner of the post
To find the owner of the post makes Inverse one to one relationship.
Add the code to the Post.php model
/* * Inverse relationship to get User data */
public function user(){
return $this->belongsTo('App\User');
}
Create a route in the web.php file
Route::get('/post/{id}/user',function ($id){
return Post::find($id)->user;
});
Output:
{"id":1,"name":"Jewel Chowdhury","email":"jewelcse045@gmail.com","email_verified_at":null,"created_at":"2020-08-12T16:12:04.000000Z","updated_at":"2020-08-12T16:12:04.000000Z"}
One to Many relationships
It means that a single user has more than one post. We have to fetch all records. So, add the code in the User.php model.
User.php
/* * One to Many relationship method to get users all post */ public function posts(){
return $this->hasMany('App\Post');
}
Create a route
/* * One to Many relationship */ Route::get('/user/{id}/posts',function ($id){
return User::find($id)->posts;
});
Many to Many relationships
In this scenario, to relate many to many relationships between two tables there needs an extra table. This table is called a pivot table. There is no need to make a model for this pivot table. The only pivot table is used for a database to store the reference. Pivot is a lookup table.
So, in this situation, We want to make user roles table, role_user table. Users different role store in the roles table. Multiple users can have the same role. And this is why the pivot table comes here. role_user refers to the pivot table.
Firstly, create a Role model by running this command
php artisan make:model Role -m
It creates a Role.php model and also creates a migration file.
Add this code in the roles migration table
public function up(){
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
And we have to create a pivot table named as role_user by running this command
php artisan make:migration create_users_role_table --create=role_user
Add this code in the role_user migration file
public function up(){
Schema::create('role_user', function (Blueprint $table) {
$table->id();
$table->integer('user_id'); // user id
$table->integer('role_id'); // role id
$table->timestamps();
});
}
Now, let's create our route
/* * Many to Many relationship */ Route::get('/user/{id}/role',function ($id){
$user = User::find($id);
//return $user->roles;
foreach ($user->roles as $role){
echo $role->name."<br>";
}
});
Output:
Adminstrative
Contributor
Find the owner of the role
Route::get('/role/{id}/user',function ($id){
$role = Role::find($id);
// return $role->users;
foreach ($role->users as $user){
echo $user->name;
}
});
Output:
Jewel Chowdhury
Post a Comment
Leave a comment first....