Laravel - Day 7 - Passing data from database from controller to view

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello what's up everybody how is it going welcome to another video of tutorial series learning laravel from scratch I'm your host Andy and I'm here today with a new topic and the topic is loading view along with the data that we fished from database using our controller so what we'll be doing today he has been creating our controller will be grabbing the data from the database and we'll be sending it to the view file we'll also be using some other tips of a lot of it where we'll be using our resource controller in order to manage our food operation much efficiently so before we before going to the AB environment starting with the project I want to show you something that you can see in our home page we have a link to login and register so if we keep this these links over here you can see that anybody can register to the system and access our admin panel so what we want to do is we don't want to show the these links over here rather we would like to create our own link and just remove this link so this link this is being shown so if you go to home layout I mean homepage hungee you first of all let us check our routes let me close out of files tabs you can see that the slash means welcome so let's go to welcome which is inside resources to use welcome inside I will come we have two links login and register you can just remove these links over here and let's just say we don't need this okay now let's try to refresh this by the way we'll be changing the layout everything from here so you just see this fresh this now the link is gone now for the more going to public slash login is a bit confusing so we can create our own generic URL there we'll be using our some generic keyword like slash admin or slash administrator or slash control very user can easily determine what is our admin panel so inside our web what I will do is I'll just copy this home and paste it over here so home shall be now equivalent to admin and admin means home controller and index function and the name shall be home no problem and just remove another hole okay now since slash admin is equivalent to home home controllers index function because let me show you something before I do this whenever we go to login and type in let's say info at the rate code I recom and 1 2 3 4 5 6 1 2 3 4 5 6 login you can see that we have each to home but what I want to do is I want to replace this home with admin so in order to do that I'm just copying the exact route of home and placing it to admin first thing we have done this secondly if you go to odd you have a login controller over here you can see that after I am logged in it is redirecting me to slash home so I want to make its last admin I just try to check our system now first of all let us log out I have a log opening at the end as we did last time so now I'm gone now if I go to my beefs URL and type in admin at the end it will try to take me to the home and since I'm not logged in it will take me to the login page so this is working fine now whenever I type admin it is taking me back to the login that is making sense now so if I type in hundred could I be calm and password is one two three four five six and click on login it is taking me to slash admin now now this this is quite making sense since we are in admin panel and our URL is less admin so that is perfect secondly what I want to do before we start our crudo operation is just modify these links so that our system looks like whatever we want to do only those things are in the sidebar so in order to that I'll open my layouts have been laid and I would go to these are our links I will just remove these different versions of dashboard that's what you want us move it to this to be three which is over here dashboard that's movie 1 this would be 2 this would be 3 so I don't need in these feelings anymore so let me just remove this and it does not have 3 more menu options so let me just remove this save it save it and this you can see now only I only have dashboard but I still have this icon over here and which I need to remove so that it it doesn't look like drop down so which is FA anger left and just remove this so that what link looks like this okay secondly we don't need to reject chart these all items we just need to middle for now category and news okay so our first link would be category so let me just add category and my second news would be the second link would be news in category I don't need this icon and news will not be a drop down it will not have any tree view and it's submenus let me just remove this okay finally let me just leave remove all the links that it has been providing us so from here to do the documentation I don't need anything let me just save it and refresh it so you can see we have dashboard category and news and finally we have labels we can log out and we can call it this labels to something different maybe something like action okay see it fish this fish this so we have action - good category in news now since we have PK our template as we want they can also remove these if you want but what I want to start now is whenever we click on category I want a category listing page and with option to add new category and edit and delete existing category so let's begin with that so let me open my terminal notice it would be command prompt go to the project CD last applications slash amps so that's number double double in your case it could be like C Drive or D Drive amps www last project now once we have reached a project it's now time to create the controller for categories category and wheels so in order to create controller as we know we can write a PHP artisan command PHP here Artie is an artisan and just like in Moodle so we have to type in a command to make column to controller and give the name of the controller which is category for category just we need to remember that if our model is singular our name of the controller should be pulling so categories would be our controller name categories followed by the controller so categories controller will be our controllers name I just hit enter it will generate a controller for us now he go and check you can see we have a new controller that is known as categories controller over here right inside our controllers folder so this open this now in categories controller will be fetching in that categories from the database or adding new category or edit editor category or delete a category so in order to deal with category we need to extend the model or use the model that categories is associated to in order to you know use that we just need to provide a use command so use since our category is inside Apfel that you need to put mention the path this way so app slash category so by doing this now we can use this product category model throughout our class so first thing I want to create my constructor that will check if I am logged in or not and since this is a cool operation which is related to admin panel I want to first of all verify if a user is logged in and in order that if you see inside our home controller which is the page where we are relative just after login so here it has been using this piece of code that checks if a user is logged in or not so it is using mittelmeer odd now and this code this one line of code will check if the user is logged in or not so if you place this inside a constructor it will before browsing any of the function you to the first of all called constructor and check for us if the user is logged in or not so if you copy the exact same hood and place it into our categories controller by doing this we have successfully created a constructor which checks if the user is logged in or not before calling any other function now we are ready to call the public index function so public function index okay now here the index shall be a page which will be responsible to call and to grab all the categories from database and list it to our view so in order to grab all the database to select each and every categories from the database what we need to do is we just need to use the same model now we can create a variable called categories and say category Polland column find to define we can say all okay so category conk on all means we are trying to select each and every entry from the categories table and returning it back to categories okay now if I want to same these data to the view file what I need to do is first of all we need to know the view so let us just prepare a view before we send it to the view so inside our views I want to make a folder called admin we will be right now placing each and every of our app in balanced view file so I have a folder called admin and inside admin I want to create another folder and call it categories so all of the view files related to category shall be inside categories folder and since this function is index I would like to create another file called index not played or PHP so this file will be responsible to list out all the categories in our admin panel so here will be loading this file so now we can say return view now we need to give the part of the view in our index function so which is admin and instead of saying slash we can just write dot novel so I had mean dot categories thought index so this is the part of our queue file and if you want to send this data to the view file you should send this as an array just like in CodeIgniter so either we can just say prepare an array like this har-har and categories and pass the array over here or we can just make an array over here so once we have prepared our array a our category is equals to category which means all categories implicit into area categories we can just say with donor in our art so what this will do is this will send this array to the view file now in our view find index we can use variable we can use this index categories as a variable in our view file so now if you go to index plane and type in command PHP and let's say var dump donor categories see it and try to refresh this but this will not still work because we have not returned the code or the route for the index file so you just let us just write it out let me just remove let us just have it now what we can say is and you can just copy this yesterday the end and we can say admins class categories shall be categories controllers index function and its name will be let's say a list underscore category okay so we have a NEMA per out and the route is referring to categories controller and we also need to write control over here so categories controller index function and you are representing this route which Champy admins lands categories so we have the URL we have the route and we have the name for the route so now let us try to browser admin / categories so hat means less categories and it is saying add category not found I'm not sure if we have already created the view models next let me check we only have news model we don't have categories model so last time we only prepared a model for from let's say news and the migration for the news didn't check the migration we only have migration for news so let me also create a migration for categories so each in is the concept here is each news shall belong to a category for example category could be entertainment sports and politics these will be the category for the news and news itself will belong to a specific category so let us create a migration for category so PHP artisan make migration then my migration would be create category Gauri's underscore table okay why don't we just create the Magnusson along with the model so it just delete this migration create another one PHP artisan make category along with my information right so once we do this modules has been created migration also has been created and my migration is categories over here so I just need a field called table and it is a string so title and I would like to add another field called slug let me just explain you about slug even inside news later just forget about slug right now but I will explain about slug in coming videos so we have a table for categories and we have a table for news so let me and now we also have a model for category I think we can start using our controller it is saying basically we have not magnitude and category hits of PHP artisan migrate okay now categories table has been created now let's try to refresh this you can see since the item is we have no increase in database so the item here is being shown as 0 why don't we just add few items in our database for now manually so now if you go to what was our database it was just go and check my environment variable and that amuses project ok this is our database now inside our categories I want to insert some of data and let's say sports and the creator add would be today and updated add would also be today to creat another one entertainment greater that would be today and that would also be today it go now we have to increase in database now if I try to refresh this again it is showing me those two entries and you can also see there are lots of other stuffs that is been generated so you don't have to worry much about it but you can just be assured that whatever we are trying to fetch from the database we are getting it in order to double and double check it what we can do is inside our view file which is next bleed what we can do instead fathom we can say for each and all other categories altano see let's say equal dollar see ID let's see if it works so in each loop we should get the ID of the category so 1 & 2 right so 1 & 2 is the ID of our categories so we are getting it so perfect but it is not the thing that we want to do inside of view first thing that we want to do inside of view is to extend layout right so at the rate I mean I've read extends our layout is dollar layout don't add me right we want to extend the layout admin we want to create a section over here so my section contained shall be written inside here so section in n section here I want to give give a heading just like in our default home page so the heading just like this and just inspect to see you what it has used so what it has done is it has used let's say container header container field all these functions so what we can do is I can just and just to make our design look much better I'm just use the code of the let's say how basic dashboard so this was the code that was used for header let me just copy this and place it inside my the listing page category listing PS I'm going to say category is over here leave a breadcrumb if you want but for now I'm just leaving this needed to say homes last categories instead of saying home we can say dashboard over here and save it and our categories home page in order to go to home page you can just say URL there is a function in Nara which is known as URL and we can get a part of the gunnison since it is blank I can just write in slash over here and it would take me to slash admin over here it can take me to slash at me so our dashboard is less admin and we are currently at category so let's just see if it is looking good so if I click on category admins TAS categories it is taking me to MIT severely out admin not found me out shortly out it's layouts okay so save it and refresh this and we can see categories in dashboard categories and if you click on dashboard it is taken to a second thing what I want to do is I also want to add a link to link to here so if I click on category it should take me to category space but it's taking me to wrong link right now so just quickly modify our layouts which is over here so go to categories which is here and add a link to it so our link is no more this what we can do is if you go to our the route page which is web dot PHP here you can see that we have given a name for each and every link so the name for the list category listing and category page which is categories index is list category right so you can just copy this name and place it over here which is in our category right so my category is here right so I'm just place it over here but then now you know what you can do is you can just call a function called route and pass the link the minute route over here so if you see here in our web dot PHP we have a URL for route route itself the name of the controller and function and the name of the route so whenever we are using the function called route we can just pass in the name of the route which is list category and it will automatically generate the URL that's called admin / categories so in our view file while generating the URL for any link we can just use we can use two ways the first way is calling a function call you and if you are calling the function called URL we should pass the URL that we created for the route this way so admins last categories if you want to use the function URL you should pass this as parameter and if we are using the function called routes you have to use the link as list category just like in case of our admin bleed here we are using a function called route so I'm just passing the name of the route and here in index we are using the function called URL so I am passing this right so even here we could have written route and name of the route would be home right so slash admin is represented by poem so even in this case what we could have done was instead of writing URL we can write route and give the name as that and give the name as home so these are just two basic functions for URL so route stands for a function that uses the name of the route and URL stands for the function that uses the and that URL of that let us say there opt okay so currently I'm using just route in both the cases so let's see if our route works so now if you go to categories you can see over here that the link for the category is admin / categories so let's just click on this and you can see that my link is taking me to admin / categories similarly over here we can see our link is to admin so we never click here it is taking me to admin so perfect my URL is working now in our category page I have added a heading a breadcrumb now I want to list out my categories which is in my let's say and database so I again want to copy the layout that we have in in our home view still continue we can be whole bleed so our content now should be inside section and Dave and flow these are something let us just copy these two copy this and place it inside my so this is content header from here to here you just leave tab just to make it aligned properly table tab paste it over here close the div and close the section line it attack so now we have a section and deep ready now what we can do is we can just create our own table just like this you can pass to a table class equals to table table - border this just for design purpose bordered or instead of saying bordered okay let's give a border and it's a table dance striped as well so we have a table ready so another thing that I want to do is I want to create a TR and deep th some th over here so copy the th and let's say give this a label call ID this is a little nibble call title and give this a little called action so we have three labels now after that what we can do is we can create a loop of those categories that we are sending from the controller so in order to use look we'll be using for each loop and in blade temp let me write for each this way so at the rate for each and all other categories as dollar see we can end the for each over here just like this and for each and you can see give TR and inside out TR we shall have three TDs TD TD Tilly so now firstly will be displaying our ID of the categories which will be PHP we don't need PHP anymore since this is bleep template we can just use a double bracket and right Donna see ID we can also have displayed an auto entry increasing number over here but I'm just using ID for now so for title wish I could write in dollar C title and for action we'll be creating two different links one for edit and one for delete okay I'll do that I'm just writing anchor href let me just place it has four now and call it edit and another anchor and call it you get hash and delete and you can also give a class beauty in between - info and for this class UTM and beating bass and let's say danger can also create another anchor just above the table and call it add new category so let me just place it in paragraph tag even an anchor acharya the post has eagle pass BTN and BTN that's primary and call it add new category let us just check to see if our listing looks fine now so you can see we have an add new category link button we have edit button we have delete button so this is our pages just working perfect so we have successfully passed these data from the controller to the view file listed it out using blade template using the same layout happening for adding new category editing a category and deleting a category so in next video we'll be learning how to make these button work and if we click on add new category it will should take let's say a farm and that will allow us to make entry for the category and if we have a click on edit it should generate a form without loading the contents of that is in the database and after we hit save it should update the content and the delete should now category that we are working with in another video I will just start another approach we'll be using the source control for all these so right now we are manually writing quotes creating functions for different for controllers we are quitting index function we are quitting we'll be creating another function but using resource control will make our work much more easy and we don't have to bother about routes and I'll just explain about it in another video so that was it for today I hope you liked the video enjoy the video you understood what I've tried to explain and if you have any confusions regarding the video you can mention it down in the comment section and if you have understood the video if you liked the video don't forget to hit the like button hit that subscribe and share the video and I'll see you guys again with another video in the in coming days so till then have a nice time goodbye [Music]
Info
Channel: Kodiary Technologies
Views: 29,370
Rating: undefined out of 5
Keywords: PHP, Laravel, Laravel from scratch, Laravel from basic, Learn Laravel, Passing data to view from controller, Laravel find all, Kodiary, Kodiary Technologies
Id: ZuFoqGfltls
Channel Id: undefined
Length: 34min 20sec (2060 seconds)
Published: Sun Apr 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.