Laravel 8 Tutorial - Eloquent Relationship Many to Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the laravel 8 tutorial in this video we are going to learn about eloquent relationship many too many many too many relationship is a little bit complicated than one to one and one two menu licensee an example of many-to-many relationship is a user which may have multiple roles where the role are also connected with multiple users all right so for understanding many-to-many relationship let's create three model user role and role user okay user model and migration are created by defaulting project so now let's create model and migration for the role and role user okay so switch to the command prompt and here just type php artisan make colon model model name which is role also create the migration so just write your dash m okay now press enter all right now let's create raw user role user model so just write it php artisan make colon model space role user okay and just add here that's m press enter all right model and migration created now switch to the project go inside the database then migration and from here just open create rows table migration okay and here let's add some columns so just write here dollar table data type string and column name name okay now save this file now just go to the role model so just go inside the directory the models and just open role model okay here just add the table name so just write your protect dollar table is equal to rules all right now save this file now go to the migration directory and just open role users create role users table migration okay and here just add the column so just write here download table data type in user desired big integer okay and just add the column name user underscore id okay also set the one sign all right now here create another column dollar table big integer column name underscore id okay this is also unsigned just right here unsigned all right now set up uh foreign key so just write here dollar table foreign and inside this just add the column name this one user id and set the references just right here references on id column of user table okay so just right here on user users okay also add here on delete all right now create the foreign key foreign key for this control id so just right here table foreign and here just add the column name this one roll underscore id and add the references on id column on rows table okay so just write here rules all right and here just tap on delete cascade all right now save this file now go to the uh full uterus model so just go inside the directory and models and here just open role user here just add the table name protected dollar table is equal to table name which is this one roll underscore users just copy and paste here all right now just close this migration also close this migration all right inside the role with a role model inside the role model just let me add here function so just write it public function function name which is users and here just return download this belongs to many so just right here belongs to many user column caller class okay and here the table name rule underscore users and now save this file and go to the user model so just open user model and here let's create a function public function and function name which is roles okay and inside this just return dollar this belongs to many and just head here rule column column class and here the table name which is rule underscore users okay now save this file now let's migrate the migration so switch to the command prompt and here just type php artisan migrate okay now press enter all right migration complete now let's create a controller so just write it php artisan make colon controller let's say controller name is rule controller okay now press enter controller created now switch to the project and just open rule controller so just go inside http controllers and from here just open rule controller okay and inside this role controller let's create a function for adding the role okay so just write here public function function name which is add role and inside this let's create a an array here so just right here and inside this array and just write here name arrow rule administrator all right now just copy this and paste here all right now change here editor author then contributor all right and subscriber all right now save this file and add your semicolon and here just write role column column call the insert function and inside this insert function uh just pass this area okay and here just return roles are created all right now save this file and here just add the role so this will use app [Music] now save this file now let's create the route for this function so go to the web.php file and just go inside routes php and here let's create a route route get function name url name which is slash add rules okay and add your controller name which is role controller all right also add here we will control it now just add your class comma function name which is this one okay now paste here now save this file now let's check this and add the role so first of all run the application so just type here php artisan okay now press enter you can see here roles are created successfully now go to the phpmy admin and just open database level 84 db2 and open rules table just click on browse and you can see here the role administrator editor and subscriber all right now let's create a function for adding user okay so just go inside the controller role controller and here let's create another function of the function fusion name which is add user all right now here just write user is equal to new user just add here the user model so you just try to use model user all right and here just write caller user add-on name let's say name is on [Music] all right now add the user password so just right here user array password is equal to call here encrypt function so just right here encrypt and inside this just set the password okay let's say password is secret all right now call the step function so just write a user head of save all right and here just create role ids array and inside this array just add here uh let's say one or one and two let me shift and then edit okay so just here the id administrator one and four editor stadia okay now here just write dollar user arrow rules and just add here attach and inside this just past this row like this okay now simply return here record has been created successfully all right now save this file and let's create the route for add user so right here without column column okay add user add users and the user is already used here so just right here add users and then so role controller column column class okay and just add the function name so just copy add user and paste here all right now save this file now let's check so switch to the browser and here just add users okay now press enter recorder has been created successfully now go to the database and just uh just click on browse in front of users okay just browse the users table and you can see here names on email id password okay and just browse the uh just refresh the targets and just browse role users okay so just click here you can see here here user id1 and roll id 1 user id 1 and roll id 2 all right now let's add one more user so just change here the name so just right here email id then for address email.com password secret and here change the rule okay let's say who note 5 all right now save this file and just refresh the page record added and here inside adjustable just refresh the table and you can see here value for user id 2 okay id 2 inside the roll users just refresh the table and here you can see user id2 uh roll id 2 2 3 2 4 okay and inside the all right now let's create two function one for the fetch all rules by user and uh fetch all user by role okay so let's do the project and just that this role controller just write here public function and function name get all roles by user okay and just pass here the user id okay the parameter and inside this let's write here user is equal to user colon find and just pass here the id okay okay rules is equal to dollar user okay just return here okay now add one more function here public function get all users column column and and here just pass the rule id okay dollar id and here the state users is equal to now save this file and let's create the routes for this function so go to the web.php and here just write about okay uri rules by user okay and just add the parameter id okay and here controller name first controller function name which is this by user so just paste here now at the route get and uri just add here users by pull okay and here just add parameter with roll id and here the controller name post controller okay here column column class also add here okay now add the function number so just copy this function and paste here all right now save this file all good so let's check sources to the browser just go to the url just check rules by users just write here rules by user just earlier user id1 okay and now press enter all right get all roles by user does not exist i think a spelling mistake so let's check just a blender just right here role controller not controller okay now save this file and let's check so just refresh the page and you can see here the role administrator and editor of user id1 if i change the user id to you can see here edit author and contributor okay now let's check the another route which is users by role okay so just add here users by wall you just add here the role id let's say one you can see here it's done if i enter here to two for the editor okay please editor so just enter and you can see here both are edited okay and if i enter here three leave for the author okay just press enter you can see one is then so in this way you can create many too many relationship in laravel eight so that's all about we need to manually elect a lesson so thanks for watching this video and if you still have any kind of question you can ask me in the comment box don't forget to subscribe my channel thanks for watching
Info
Channel: Surfside Media
Views: 10,870
Rating: undefined out of 5
Keywords: How, to, Program, laravel (Software), laravel, laravel v8, laravel 8, laravel project, laravel setting up, laravel v8 setting up, laravel v8.0, v8.0, laravel 8.0, laravel 8 playlist, laravel 8 tutorial, laravel 8 lesson, laravel tutorial, laravel 8.0 tutorial for beginners, laravel for beginners, laravel tutorial for beginners, eloquent relationship in laravel 8, relationship in laravel 8, many to many, many to many relationship in laravel 8, laravel8 many to many relationship
Id: ymYZA4N86oo
Channel Id: undefined
Length: 23min 3sec (1383 seconds)
Published: Thu Sep 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.