Laravel tutorial #24 fetch data from database | Eloquent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back to the laravel 6.0 video tutorial and in this video we are gonna learn that how we can fetch data with the eloquent model all right so all about the basics of eloquent model I already told you in last video if you want to start with that then you can watch our previous video so now let's see the main points of this video so first of all we will make a model after that we have to make a controller and the router for it and then we will import our model inside the controller then we will fetch the data inside the controller and after that we will pass the data to the HTML view alright and after that we will apply some wear condition order by and limits and etc so this is a flu slash video for get that data from database and render it on the HTML all right so now let's start with the code so first of all I am just going to make a model for that we must have to know that what is our table name so that we can make the model so I am just visiting my PHP my admin so and here you can see that my table name is users all right so I want to make my model name is user because the table name should be the plural of our model name this is the standard of the lara alright and the database configuration i already did in my last video so let's make a controller PHP artisan make model user and hit the enter and now you can see this model inside the app here alright so this is our model is ready now so after that let's check to check out the second point now we have to make a controller so it is right here PHP artisan just remove it for the HP artisan make controller and the controller name my controller name is users and hit the enter now you can see that controller inside HTTP controller and let's refresh it once and here you can see that all right so after that I am making a route for it and I have to just go from the web dot PHP right so I'm just going to remove my previous route and making a new one so that you can see it from the very scratch get and this is a database list so I am just going to put the router name is list and user at the rate list this is list is my function name it again so alright so now I'm going to open this page and making a function inside the controller just make a function list and in this list I can simply write something echo like echo code will be here so I am just going to print it once more once so that we can check that this is working or not so let's put here list and let see where is the error yeah because I have to put it here users that's it so now you can see that the code is here okay after that let's see the next point I have to import the model inside the controller so let's use here use app and my model name is user alright and now very simple thing is just right here return model name and all right and now let's see now you can see that we got the data here is it the same data here inside the table yes there is a two columns here and we can see that them here so let's say I don't want to connect this with the users table that how we can connect with the manual so I want to connect it with the company table for that but we can do for that just go inside the model which is the user and just right here protect it protect it dollar table and table name which is coming okay and now let's check the output okay select star form yeah I just misspell the company now you can see that we caught that data from the table tell me all right okay so this point is also covered now let's see the next point we did that also now let's make a view and pass the data to the view for that I just have to go inside my resource folder where we have to define the views views and just make a new view like user door blade dot PHP or you can put their user view dot blade dot PHP all right and just save it and here just write a normal DB you can put your complete HTML but for the quick video I am just going to put a div alright and this right here view page here and inside your controller just remove for now this commend it for now and this right here return view first column is your name and that's it all right and let's see what is there now you can see that we are able to access that view also so now uncomment this code take it this data inside the variable like data and pass it as in second parameter and second parameter data should be kind of array so just right here data or you can take anything but if you want data so now this data is passed here so now how we can get this data for that just right here ul and inside there just take a for each loop at the rate for each this is our the by if you don't know that's why we put that at the rate then you have to watch our previous video about the front end layer blade template the link is given in the description box alright so just write here the taller data as dollar I let's see and at the end this right here and for each nice and in the middle just take a li here and just write your dollar I name and now let's see the result now you can see that we got the list of all companies two three four five three and three six companies are here then you can see that there is a total six of result all right so we are complete or successful these things now let's see we have to put her where condition so let's say I want to get the data where let's say address is noida all right so just go inside the view here and right here where address and second parameter is no ADA after the coma obviously and just put here get function alright and now let's see now you can see that the same second info gained only based in Noida and the same thing is here okay so now let's say these companies are we don't know the order of these companies so if I just remove it simply remove it from here we don't know the order of so let's say we just need two ascending and descending order with the behalf of name then we can just simply put it here order by and pass the two parameter one for the field name which is named as second for ascending ascending or descending alright so just right here and arrow right and let's see now you can see that the ascending order and if you want to see in the descending then right here de okay this is a descending order so let's say if you need to get data only up to the limits will just take use your take function and inside that take function this right here to only and now you will get only two data here if you just put here one then it will put only one date I here if we just put here zero then you will get nothing all right so this is all about that how you can get that data from database or fetch the data and how you can pass to the view and make a list so thanks for watching this video and if you still have any kind of confusion you can ask me in the comment box don't forget to subscribe my channel thanks guys bye bye
Info
Channel: Code Step By Step
Views: 40,300
Rating: undefined out of 5
Keywords: fetch data laravel, laravet fetch database, laravel fecth data eloquent, Laravel 6 tutorial, laravel eloquent, laravel tutorial, laravel 6
Id: 0KM62xCR074
Channel Id: undefined
Length: 10min 31sec (631 seconds)
Published: Tue Sep 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.