Laravel 7 Tutorial - Eloquent Relationship One to Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the level seven tutorial in this video we are going to learn about document relationship one-to-many one-to-many relationship with used when one table associated with multiple for example a post may have multiple comments so for understanding one-to-many relationship I will just create two table both stable and a commenter tables okay both table are connected with each other so let's create so go to the manhunt and there here just type PHP Addison make : model is model name model name is host and also create the migration so just write here last M all right now press Enter model created now it's creating the migration all right migration has been created now gate model and migration for the comment table so straight-edged PHP add listen make model is model name your model image come in and also create the migration so just write here - M and now press Enter right now post and comment about a model in my kitchen I have been created so now go to the post table migration doses to the project and just click on database then migration and just click on create post table migration okay and inside this migration you just add some column here so just right here dollar table arrow data type string the column name let's the column name its title alright up hit another column table this time dread I paste text and column name let the body alright now save the file and go to the comment migration so just click here create comment table migration and adhere some column so just right here dollar table arrow int big integer and column name lexico surname is post ID also sign right now double arrow string and column name win alright also at the foreign key so just right here table arrow foreign and here host ID and references with ID on for stable so just right here on Google name hold all right now save the file and go to the post model so just go inside a folder then click on post yeah first of all add the table name so just right here protocol that a table sorry protected a space dollar a table is equal to well name hosts and here just create a function public sungchung function in a means and inside this function just to return dollar this arrow has meaning inside this just right here comment : : class all right now save the file and go to the comment comment model you just open this file and here just add the table name protected dollar table is equal to comments all right and also create the function so right here public function function name post and inside this method just returned all are this arrow belonged to launch to post class so just right here and post all on one class all right now migrate the migration sauce to the command prompt and here just write PHP artisan migrate okay now let's enter all right migration for stable and commentable migration has been have been migrated okay so now let's create a controller so just right here PHP add this then make column controller space dollar name it's a controller name is post controller all right now press Enter alright controller has been created now go to the bus controller social kill switch to the project and just click on a folder and then HTTP controllers and inside this control and just click on open folder ok here side the post controller just add a function for insert record so this right here public function function name it's a function image add post right now just right here dollar post is equal to MU pollster alright and also add the post model here ok use app flash ghost all right now dollar post rotator is equal to let's say title is first post all right and dollar post arrow body is equal to let's say first post description all right now save the post so just right here post arrow call the save method all right now return a message post has been created successfully all right now to create another method for the adding comment so just write a public function and comment all right and inside this method you straight air dollar post is equal to host Colin Colin find let's say just pass here ID so pass here the parameter Eddie okay and just right here dollar ID all right now dollar government is equal to new comment all right also adhere and dollar comment arrow comment is equal to it's a comment is this is first comment all right now save the comment so just fall post controller post arrow comments and all the Scimitar inside this image the tip method just pass here comment dollar comment alright and just return the message return come in has been created successfully all right now we ate the routes for this method so go to the wave dot PHP file inside the rod folder so just click on routes then wave dot PHP and here just create routes first route it and let's say for I is add post controller name post controller and method him let's copy the method name and post okay then just is here next year create another route route add comment and pass the parameter here let's the parameter is ID and here comma controller name host controller n38 method name and comment ok so just copy and paste here alright all done now let's check so add some post here so go to the browser and here just call add post okay now press enter you can see here post has been added so go to the be my admin just reverse the database and postable just browse here and you can see here first for first post okay now add the comment so just write here add comment and add this comment to the first one so just I pass the ID one okay now press Enter you can see your comment has been added yet it now just to go to the database and click on comments tables we'll just browse the comment table and you can see here post ID one and this is comment alright now it's the comments by post ID so for that just to go to the post controller and here yet another method public function it's a function Amy's gate coming by post all right and I'll share the post IDs so just right here caller ID and here the try to comment is equal to post Colin Colin find us here the dollar ID and arrow moments right just return dollar comments right now create the routes so go to the web dot PHP and here just create route it let's say where I is get comments all right and pass here the host ID so just parameter Heidi controller name post controller host controller at the rate with her name so method name is it comments my post so just copy and paste here right prom ticket yet comments this okay now save the file and it's sick to go to the browser and here just write it come in with these comments okay and so just right here comments and pass the ID so just right here one post ID of the center you can see here in the comments if I add another comment with the adhere this is thick comment alright now save the file and add one more comment so just right here localhost add comment all right now presenter comment added and if i refresh the page you can see here all the comments okay so this way you can use one too many low current relationship so that's all about one-to-many the quaint relationship thanks for watching this video and if you still have any kind of question you can ask me in the comment don't forget to subscribe my channel thanks for watching
Info
Channel: Surfside Media
Views: 6,578
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, one to many, relationship, one to many relationship in laravel 7, eloquent one to many relationship
Id: iarUvRk6WCw
Channel Id: undefined
Length: 16min 23sec (983 seconds)
Published: Fri Apr 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.