Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e48 – Eloquent Relationships Many To Many Part 2 (BelongsToMany)

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e48 – Eloquent Relationships Many To Many Part 2 (BelongsToMany)

welcome back to part 2 of the many to many relationships in laravel if you missed the first part check out the first part before you move on to part 2

in this part we're gonna be talking about attaching data to the pivot table and this is useful because sometimes there is relational data that is only relevant if two things are in sync with

each other so the example we're gonna use is you have a user and then you have a role but I also want to know who gave that user the ability to perform that action meaning who was actually

responsible for the sinking of that so that's what we're gonna be working on we're gonna add another column to our role user table and then that's gonna store a name and the name is gonna be

the name of the person who performed the action to allow that user to perform that role it's a very common thing to do because a lot of times you'd want to be able to know who granted the permission

for someone to do something in your application so let's work on that now let's go to the migration for the create role user table and let's add a new column here and we'll say table and the

new column is going to be a string and the string is just gonna be named as in the person's name alright so now let's do a quick PHP artisan migrate fresh so we have a nice and fresh database and

there we are let's jump the table plus and of course everything here is empty except now we have this new name so how do we do that alright it's actually quite simple let's

continue with the example that we had going on before this one let's comment that out let's get rid of this there we go alright so we'll just go back to just

using sync so we have a user we're grabbing the first user from our database actually that reminds me we actually don't have a user anymore so we need to make one now let's jump to PHP

artisan tinker and just make one really quick factory app user class creates perfect so now we have a dummy user great alright so now that we have a user we're going to fetch that user and then

we're gonna attach this role with ID of three which actually reminds me we also don't have any roles anymore let's take care of that now very quickly rolls let's

just add one and we're gonna say remove users as that's going to be the role and of course that has an ID of one so let's go back now we can say the following instead of just passing in an ID let's

actually use the ID as the key and we're gonna say one and then any relevant pivot data is gonna go right here who performed this action I will just save Victor so Victor performed the action of

attaching user number one to the role with an ID of one great let me hit save let's go back to Chrome hit refresh and now let's check out table plus so if we go to our row user table we hit refresh

we now see though of course we attached role ID one with the user ID one but now we have this other data attached so Victor was the one that was responsible for attaching that together so that's

pretty cool now how do you retrieve this data let's go ahead and just comment this out for now so if we grab their user actually know what we already have it up here we'll grab our first user so

then we can simply dine dump user give me your rules and if we go to Chrome there we are so user roles of course we know that the one role that this user has is just the remove user's role but

where is Victor where is that column how do I grab that pivot okay so there are actually several ways to do it but I'll show you the easy way to do it so we have this R users roles let's grab the

first row that way we're only working with a single row let me show you that in Chrome there we are so you see we have a role instance in the role instance of course

like I said is the remove users so instead of having a collection now we just have a single role alright so now that we have a role let's just call pivot on that role and let's check out

what we get refresh and there we are so we have a role yeah that's pretty cool right so we have the user ID in the role ID however we're still not seeing that name column

how come we're not seeing that well that's because we haven't told laravel about this extra data all right so to do that let's go back to our model this user has many roles it belongs to many

roles and it does have with pivot it does have name all right and we're gonna do the exact same thing to our other role model all right paste so there we go now we've told

laravel hey there's an extra piece of data on that pivot table that I want you to grab with you so let's go back to Chrome hit refresh and now there we go so now laravel is aware of that name so

now we know that the person responsible for joining these two things together is Victor great in the same manner if we go back to here we can't just call at this point just named so pivot name it

refresh and there we go that's how you retrieve any additional data from your pivot table this is very useful and you can have as many columns as you want and again the pivot table is going to hold

any data that is only related when those two models have a relationship so any data that is only pertaining to that relationship is what would go inside those columns so this is a pretty cool

example and just shows just how powerful level and eloquent is when it comes to databases so with that we're going to conclude the explanation of the many-to-many relationships and laravel

go ahead and implement that in your application and get comfortable with it and when you're ready let's move on to the next episode

Eloquent offers a simple way to declare database relationships. In this episode, we are adding additional data to the pivot table and storing the name of the user that performed the sync of those two resources.For the best experience, follow along in our interactive school at https://www.coderstape.com

Code for this Lesson
https://github.com/coderstape/laravel-relationships/tree/many-to-many

DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d

Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch

Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)

About This Course

Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue