Laravel 7 Tutorial - Eloquent Relationship Many to Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the laravel 7 tutorial in this video we are going to learn about eloquent relationship many to many many to many relationship is a little bit complicated than one to one and one to many relationship an example of many too many relationship is a user with may have multiple roles okay where the role are also connected with multiple users all right so for understanding many too many relationships just create three model and their migration one is user model second role model and third one is role user model user model and their migration is already created in my previous video okay so now just create model and migration for the role any role user so for that switch to the command prompt and first of all stop the running application so just type here control plus c and just clear the screen so just type your cns press enter now let's create the model role so just type here php artisan command which is php artisan make colon model and model name which is role okay also create the uh their migration so just write here it as enter all right model created successfully now create model and migration for the role user so just type here okay it's processing still processing wait all right so just type your php the space artisan make colon model and model name is role user all right and also create the migration so type here dash m now press enter right mode model created and migration created okay all right now to run the laravel application so just type here php artisan server all right now it's running on localhost this okay eight thousand port so switch to the browser and refresh the page all right it's running now switch to the project and here inside the app folder you can see here the role model and role user model okay so just open role dot ph role dot php you can see here and inside the database folder migrations and inside the migration you can see here create role roles table and create roles user table okay now just open create role table migration so just click on this file and inside this migration just add some column okay so just right here this is a id column and this is time and stem now add one more column here which is table data type a string and column name is name okay role name all right now save the file now go to the role user migration just click on this file and here also add the mic some column so just right here table here and i think just write your begin big integer okay and just write here the column name user id unsigned dollar table big integer and right here the rule id okay unsigned [Music] all right now just create here the foreign key so just after this right here table and foreign inside this add the column name which is user id and references with id okay on vegetable user table so just type you can see here the user migration which was created in previous video this one okay and this id column all right so just type here on vegetables user tables table name was users okay so just copy and inside the single quote right also add here on delete and pass here skate all right one more table foreign and head here the role id role underscore id references with id on roles table okay this one table so just copy table name and add here list here on delete escapes cascade all right now save the file save the migration now go to the role model so inside the app folder you can find here role model okay so just open this file and here just add the table name so right here dollar first of all write protected dollar table is equal to one name which is uh rules table okay this one rolls so just right here just paste here all right and also create a function here so just write public function and function name which is users and simply return here download this arrow belongs to belongs to many okay and had here the user not within code okay just write a user colon column class and inside add here the role users html name this one just copy and paste here okay now save the file all right now go to the role user model and just open this and also can also add the table name so just write your protected dollar table is equal to and here the table name which is the world users all right now migrate the migration save the file all done all right so now migrate the migration you can check the env file database has been configured okay database name is studentdb okay now switch to the command prompt here uh just to stop the running application now migration just run the artisan command which is php migrate all right now press enter all right now the migration has been migrated for saying migration into database just go to the browser and just write here localhost plus phpmyadmin and inside uh hp my admin just click on database database name was enb file studentdb okay so just click on the database studentdb just open this database and here you can see here the roles table and role you just table okay this is the user table all right now let's create a controller so just go to the command prompt and here just write the php artisan command php artisan make colon controller let's say controller name is role controller okay now press enter controller created now go to the role controller just close all the file so inside that http and then controllers and you can see here the role controller so just open this all right now here's uh just create some function so let's create a function so just type here i am just going to create a function for adding role okay so just write here public function and function name let's say function name is add role all right and here you just create a variable rules and inside the array just pass here the sum rule first one is administrator let's say admin administrator okay second one is editor third one fourth one contributor what and subscriber these are roles okay now just add here after this use app slash model name which is okay now just head here roll colon colon insert inside insert just pass here the rules okay this one and simply return a message roles has been created all right now eight routes for this uh this function so just go to the wave.php inside the route folder okay routes then wave.php okay and here just create a route colon colon get and just add here head rolls okay and prepare the controller name which is rule controller okay so just copy this method name and paste here all right route gate address and this now save the file and let's check so just run the application so just right here php artisan sir now it's running just go to the localhost 8000 port and refresh the page now add the uri which is add head rolls okay so just right here add rolls and press enter column not found all right all right i am doing here a mistake so let's correct this so just pass inside the roles create area of array okay so just write here name and pass the value here which is administrator all right comma now copy paste fourth time all right now just right here the editor contributor and subscriber all right now save the file and rerun so just go to the localhost 8000 and add roles now press enter okay now roles has been created let's check inside the database so just refresh the page and inside the roles table just click on browse and here you can see administrator edited contributor and subscriber all right now assign role to the users so go to the project and just open user controller okay so inside controllers folder you can see here user controller okay this controller has been created in my previous video so here just create a method just write here public function and function name let's say function name is add user okay and here just right user is equal to new user all right there dollar user let's say name is equal to amazon [Music] dollar user name for email is equal to zone at the rate of gmail.com right now set the password so just write here uh dollar user i have a password is equal to uh call here the encrypt method so just write your encrypt and pass here the password secret this is the password all right now save the user so write your user save all right now assign the role id so just write here create a variable role role ids is equal to create an array let's say pass administrator and editor okay now set this to the user so just write here user goals and just attach the record with this this one these role ideas okay now simply return the message so just write here return records has been created all right now save the file and let's create the routes for this method so go to the web.php file and add one more route so just write here route get and here add as new user and controller name which is user controller so just write your user see capital user controller add the date method name which is add user all right now save the file and it's run and add a new user so switch to the browser and here just add the url which is add new dash user okay now press enter undefined method app user roles okay let's check go to the user model and okay simply add here the goals method so just write here public function and create a rules method and inside here just return dollar this arrow belongs to many and um table name is this one roll users okay double color and and here rule users okay now save the file all right now let's check again so just to go to the localhost 8000 add new user now press enter integrity constraint violation duplicate entry okay default remove the existing record so go to the users and here just click on empathy okay and inside the alright so first of all rule users [Music] cannot truncate all right first of all remove the comments table okay so just click here all right now post which one post rules and all right now all record has been removed from users table all right now redone this all right you can see here records has been created let's refresh the table and you can see here it's on and this record okay now go to the role user table and inside table you can see here two records okay now add one more user so go to the user controller and change here the name next feature email id twitter gmail.com password this and change the role id let's say t go to the rules table and and here three four five other contributor and subscriber okay so just right here three four and five all right now save the file and here just run record created okay so take the user table okay add it and inside the rows user table you can see here three record has been created all right now let's create function one for the page all roles by user and another for the fetching all users by their role okay so just uh go to the project and just open user controller and here hit a function so just right here public function function name let's say function name is get all roles by user okay and just pass here the user id as a parameter ready and just write here user is equal to user colon colon find and here just pass the id all right dollar roll rolls is equal to dollar user and just get the rules okay and simply return rules okay all right now create another function for getting all users by their role so just write public function get all users by pass here the role id and just write here roles is equal to role colon colon find and pass here the id okay first of all add here the model so just use app splash roll all right okay now just add here create a variable users and dollar roll arrow users okay and simply return dollar this all right now save the file and let's create the lots for this method so go to the web.php and here just write route get forward slash goals by user okay and add here the parameter id then controller name which is user controller and create method name so go to the user controller and just copy this name and pass here based here okay all right now add one more route okay and users by row and one and here the parameter user controller and create a third name just copy this method name and paste here all right all done now let's check so go to the browser and here just add rule by user roles by user so just copy this route and paste here and pass here the user id so i'm just going to pass your user id1 you can see here the roles okay by the user id1 which is administrator and editor all right okay now pass here the user id2 and you can see here the contributor and subscriber all right okay now check another one which is users by role just copy this uri and and paste here and just add the one with this roll and you can see here only one user if i pass here the three okay you can get here the filter if i create one more user and just add here let's say xenifer and email id and let's assign all all the rules okay all right now save the file and just call the add a new user so just in new tab this press enter user added you can see here inside the users table your role user stable just refresh this table record inserted and set the user table okay jennifer has been added here so just refresh refresh the page you can get here to user okay which one peter and jennifer and if i press pass here one then get results on it okay so this way you can create many too many relationship in laravel seven so that's all about many to many relationship eloquent relationship in laravel 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: 1,749
Rating: undefined out of 5
Keywords: How, to, Program, laravel (Software), laravel, laravel v7, laravel 7, laravel project, laravel setting up, laravel v7 setting up, laravel v7.0, v7.0, laravel 7.0, laravel 7 playlist, laravel 7 tutorial, laravel 7 lesson, laravel 7 introduction, intro to laravel 7, laravel tutorial, laravel 7.0 tutorial for beginners, laravel for beginners, laravel tutorial for beginners, many to many relationship in laravel 7, many to many in laravel 7, many to many eloquent relationship
Id: x2_EcvJzjM0
Channel Id: undefined
Length: 36min 35sec (2195 seconds)
Published: Sun Aug 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.