Laravel Eloquent | Many to many relationship from zero to hero .

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we will learn together more about minute to mini relationship from zero to advanced level here I have two models post and tag models right and here is my data inside tags I have 20 tags here and for posts I have 10 posts so first of all what actually meaning to mini relationship mean it means May one tag belongs to multiple posts and may multiple posts belongs to multiple texts let me explain to you so first of all imagine I want to assign this tag to a post right back to structure and we may decide it may be one-to-many relationship so I will add here post ID right it will be integer enter here and save all right back to my table refresh here our post ID I want to assign this tag to the first post right but what if I want to assign the same tag here to another post here's what does minute to mini mean it's not possible using has mini relationship so let's again remove this column and to create the meaning to mini relationship first of all we need to go to our models here in my post models and function tags here and the return this belongs to mini function this function here and you define our tag or our model we want to relate Clause right and the same thing here for our tag models but in this case we will relate to posts right return the same thing this belongs to many and post clause if you notice here the second barometer is table so to create a minute-to-man relationship between two tables we need to create a third table for them right automatically a lot of it to create the minute-to-man relationship table you must follow the order of letters so in our case we have two models post and tag here our P before T right so let's create our table hyphen hyphen C back to our migrations here's our post tag and the first table or the First Column will be foreign ID for post ID and second one will be for text tag ID all right now let's migrate it and it's migrated back to our table here is our post tag table right this one here is empty and back to our controller here let's add semicolon here here's our post controller because we have already samples and some tags I want to attach some tags to a post I created this in the pointer here minute to mini let's rename it add text to post and I want to add text ID one two three this tags here my text table these three tags to the first post and now to attach data or this text to a post we can use attach method but first of all I want to inject request here request and is they undump our request and use our text as array so we can use post tags our relation and attach here it may attach one or multiple parameters or tags so we can use request tags deleted and return attached right here is our post tag table is empty and now try and attached back to our table again refresh and this is our post ID number one has tag number one and also the same post has another tag here and has three texts if you notice here we have created it and updates at timestamps but it's not created automatically so to make them filled automatically we can go to our model here let's add post and use method with timestamps right now let's try again attached refresh and now it's filled if you notice here the data is duplicated right or repeat it so here this post has this text and the same post also have the same text to avoid duplication in minute-to-man relationship instead of attaching method here you can use sink all right and now refresh back again our table has extra data by the way this sync method is adding the attribute which contains and remove other data so here for example if I added only the first tag refresh and its sync by deleting other data except this because it's one also and now refresh and as you see only tag id1 has been inserted or synced let's add one two means add only these two IDs and remove others so refresh refresh again so if I try to add 3 for example it will add 3 only sync 3 only and the remove Azure tags refresh press send the request refresh and as you see here what if you want to sync without deleting data we can use method sink without it detaching as you see here right so it means if it exists keep it if not exist attach it without duplication or removing others so three four five refresh three four five just send three only No Effects no deletion if we try it with only sync send again refresh and here is the difference between sync and the sync without detaching back here send without detaching and three four five what if we duplicate five also send refresh we have no duplication right what if you want to delete a value from here you can use the touch method so here detach and you can delete one or more ID back again and I want to remove the ID3 here send the request back to our database refresh and three will be deleted as you see here now let's fetch a boost with its text right here I prepared this endpoint and in our controller I will return here post loot tax relationship back to our Postman refresh and here is our post our text here as you see tags here right and if you notice here this pivot pivot represents the data inside our pivot table here post tag right so you may insert or store extra data inside this table and it will come under pivot if you don't like this pivot word no problem you can change it from here using as method and enter any name you want post text refresh here instead of pivot we received post text here right and we have multiple so each both tags represents the ID of the attack as you see here another for tag id5 sometimes you may need to add extra data to this post text table for example I want to add a new column called edit boy to represent the user who added this text so first of all let's truncate our data here right and add our column called edit boy edit by and answered enter here not nullable and save back to our data here is our new column let's move it here right now let's try again to create text and we will face this error here it must be fillable added by but how can we fill it as you see here we send three values or three tags let's simplify it and send one right back to our code and it must be like that so here text any billboard here our tag ID and it must be array column name here and value so back in our case our tag ID here is one right and our column name is added by here and for example let's say that 2 1. or to set it also for sync tags here refresh attached back to our table and here edit by user 2. let's make it Dynamic so here one two three and here our footage for each let's just find a new image tags right and for each request tags because these are arrays as tag here right here tags push array or tag ID here right this represents tag ID tag ID and our added by column will be at for example current authenticated is 2 or we can use OS ID to make it dynamic okay now let's dive and dump our tags back and refresh and here is our result tag ID number one added by null because we have no authenticated user let's authenticate quickly also login using ID number three for example refresh and here is our data tag id1 this one Tag 2 tag 3 here and added by user ID3 let's change for example five and here is the result change so let's remove DT save back to our data and it works five two three but if we back here to our list endpoint this one and this in the requests are here we can't see our added by attribute here this column right we can see it in the response and to show it you can go simply here to our post model and we can add with pivot and you can add any columns you want added boy as a string at it boy back again refresh and here our added boy attribute I hope you found it helpful for you support me by clicking on like share and subscribe see you in next video
Info
Channel: Laravel boy
Views: 7,580
Rating: undefined out of 5
Keywords: Laravel, learn laravel, php, laravel boy, advanced laravel, laravel tips and tricks, laravel tips, laravel advanced tips, learn php, laravel, PHP, web, laravel helpers, laravel custom helper methods, master laravel, laravel route, laravel tutorials, laravel display file, laravel clean code, clean code php, laravel performance tips, laravel advanced eloquent, laravel relationships, many to many
Id: -PNvAuDgusM
Channel Id: undefined
Length: 14min 3sec (843 seconds)
Published: Fri Dec 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.