Laravel 10 full course for beginner - using laravel query builder

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's now use the query Builder to do all the crud similar to this okay so for that we have this documentation which says that now this time instead of directly saying what you are going to run like insert update delete or select you first Define the table and then you can use some of the built-in functions which will be very handy and easy to write so first when you have to get all the user from the database you use get command after this table defining the table on the DB facade so let's do that I have to fetch all the user so instead of this select I say DB then defining the table which is users and then I say hey I need to get all the users obviously we have the diagram for this let's go reload and see we are getting here all the user which we have in our database but there is one thing to notice this is actually not a simple array it's a collection of array now collection is a level specific thing which is having more power than a simple array consider as the bunch of different different array which is the collection of an array where you can perform various tasks okay so this is how you get a user but what if you want to get a user with a wear condition so you don't have to say where because we are not writing a raw query so there is a nice wear function available so we say where so first you have to Define what you need to do and at last you say get so what is the wear condition where ID is uh let's say one okay now if I go and see you can see still we are getting the same result because we only have the one user but the wear condition is working this is the power and how do I know that there is aware Condition it's given here the where or you can go to the collection documentation and here you can find the collections we have yeah so you can see all the collections we have now here we have the wear condition like this one we have lot more amazing collection features collection methods available here so you can try and see all of them so now let's see what we can do with the insert how we can insert so let's go at the top here you can see we have the insert statement we had the insert a statement we can say DB table insert in the data and the array of the data so here let's comment this one or actually we need this so we are going to fetch all the users so I can remove the wear condition and now to create new user using the DB facade I can simply say user DB colon colon table and the table is users and now I say insert and here I am going to define a proper array of the field and the value so name is sarthak then comes the email is sarthak at with films.com and then the password is going to be the password like this okay so this is done so let's see what we are getting in the response so here we are creating it's storing the response and the diagram the response go to here reload and this time it says duplicate entry okay once again the duplicate entry so I can say one and betweens.com and boom we're getting true so if I try to fetch all the users like this and yeah we are now this time getting two users as we know it's very simple so if we have the inserted statement we should also have the update statement and you guessed it right you need to use the table and then update and whatever field you want to update so easy and I can just write it very very quickly so that you don't have to be like understand and making your time valuable so I say hey I need the email to be ABC at bitfilms.com but where if I do this it will change for every user I need to First Define the where condition where ID is what is the ID let's see that is five ID is 5 then update this okay let's once again get what we are getting as a response one we are getting now if we are getting one then we can easily check for all the users and yes the second user is now having ABC at bitfreems.com cool and I think you guessed it right how about deleting a user so it's not a big deal now because we are becoming Pro on this so instead of where condition of update we can finally say delete and we don't have to Define anything inside the delete because we just need to fetch the user with the id5 and do the delete part there so delete 1 that means we can get back fetch all the users and now we are getting only one user okay so you will say that so the there is no big difference in between using the DB facade normal query and using the query Builder it's just having some functions what is the real power of using the query Builder so to understand the real power of query Builder you need to see this documentation where it is given that yes you can do the fetch part but what if you want to get the first user from the collection from the different users we have so instead of doing this I can say hey give me the first first user so if I reload you can see I'm now having a simple object of the user and this is the first user I get from The Collection or from all the users I have this is very very handy what next you can also fetch some of the value instead of selecting in all you can say select email so yes you can do that in a different way but it's really useful and very handy now suppose you want to say where user is uh like this I can say hey where ID is one I need to fetch the user where ID is one okay so how do we do this it's very easy we did that and we are getting it but with the query Builder you have very nice function which is called find with ID of one that's so amazing so clean that you don't have to write two different different methods like where then defining the field the value and then getting the first so clean this is the real power of using the query Builder it's not just like that you can do the plug that means you can just get the title and then there are chunking system you can have thousands field in your database but you can get the first 100 work on that something and then get the first next hundred and next hundred and that will actually reduce the load on your database and on your PHP memory so these are the things which are really useful and that's why I consider query Builder more powerful than the raw query obviously you can do all these things in the Raw query actually behind the scene obviously these methods are doing the raw query but you don't have to worry about writing the raw query and even if you know the raw query you can see that it's really difficult to write the raw query instead of doing or having this autocomplete features like this so if I say hey I have this I can do the first hit enter and auto completed so these are the benefit of using the query Builder over the raw queries now this is the time to really look into the amazing feature of laravel which is called eloquent and now let's get started with eloquent and C how actually you can use the model to do all the crud part in your laravel application
Info
Channel: Bitfumes
Views: 13,076
Rating: undefined out of 5
Keywords: Laravel 10 for beginners, Learn Laravel 10 from scratch, Laravel 10 course for beginners, PHP framework tutorial, Laravel MVC tutorial, Blade templates tutorial, Laravel routing tutorial, Eloquent ORM tutorial, Laravel database tutorial, Laravel forms tutorial, Laravel authentication tutorial, Laravel authorization tutorial, Laravel validation tutorial, Laravel CRUD tutorial, Laravel REST API tutorial, RESTful API tutorial, API development tutorial, laravel routes
Id: xuR6HGVDWm8
Channel Id: undefined
Length: 10min 3sec (603 seconds)
Published: Mon Mar 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.