Laravel 10 full course for beginner - running raw sql queries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we understand what is MVC pattern but it's very important to understand like how we can actually run a raw SQL query we don't want to do fancy stuffs what if I just want to run a normal SQL query for that I'll take you to this section of database so if I click here you can see we have this getting started now when you do the get started it will tell you like how you can connect the database how you can Define the database like username Fields all of these things but now we have a very nice thing called Running SQL queries so how do we run a SQL query inside the level we have to use a DB facade so consider DB facade is like a class which is going to interact with your database and how do we write the query you need to just do something like this DB select okay so if I have to fetch all the users from the database how do I do that and to show that thing I will take you to web.php file and this is the welcome page which is this one but now I'm going to comment it and I will introduce you with the DD thing which is a dump and die so first it's going to dump and going to kill the code at that point so what if I say sarthak if I save it go here reload you can see it is having sarthak and the best part is it is it is telling you like where this DD is so it's inside the routes web.php at line number 19. okay so we will use this but first I need to fetch all users how do I do that so I say users is equal to we know we have to use the DB facade and make sure it is imported and make sure it is facade slash DB then after that let me Zoom this for you after that I have to say select so inside this as a string I will run my query so I say select everything from users okay so now we have the users all the users will be stored inside this user's array and I'm going to now dump this users here and for that I will now reload this page and you can see I have one array of users and each array include the user detail all the table information is now printed here so this is the power of the DB facade running SQL queries but this is just for selecting what if I want to select a user by id1 so how do I say so it's like very easy select everything from user where ID is equal to 1 is this going to work let's see it's working but what if you have to select with the email field so where email is sarthaket bitfilms.com now at this point it is giving error why there is a error because it's it can't actually differentiate between this as the part of the query or as the value for this email field so can we do that like this with a backtx still it's not working it is considering it as a column so what what we have to do we just need to say question mark here and as a second parameter we need to pass whatever the bindings we need so first binding is question mark replaced by this email field so now if I go reload you can see we are getting the data back because we are using the bindings and now laravel is not get confused with the email as the column name okay so we get the user so we can get back like all the users like here okay now it's time to create new user create new user how do we create new user so we have to say once again DB but this time we need to go to the documentation to see how we can insert so here is the running insert statement you need to use once again the insert you can say function and then run the query and similar to previous state we have to use the question mark whenever we pass some bindings or the values okay so it's going to be very easy we have to say insert and inserting inside the users table so insert users into users insert into users now what we have to do we have to define the field name so inserting the name email password now this is good but what are the values you know value we have to pass as question mark column comma question mark comma question mark because as a second parameter we are going to Define all these three things so first is sarthak second is sarthak at bitfumes.com and third one is pass word okay so you see we are doing something like this is it perfect seems like perfect but you said that you may have this question like why only these three Fields why not all other field like ID created updated at so if I open the migration for user see ID is auto generated so we don't have to give ID name field we have to provide it email field we also have to provide it email verified at field is nullable so it can be null so we don't have to provide password we need to provide and these two will will also be taken care by laravel okay so now we have this and let's now see what we will get when we insert a user so click and now it says that user email unique so email I have to change so like this okay and it says true congratulations you have inserted First Data into the database now I will comment and fetch all the users from the database and now see we have two user and first one is the old one and this is the new one with this ID email ID of sarthak1 bitfilms.com cool so this is really great now we have done the create part how do we update the user so we need to use use the update user so once again this has to be related to DB and I can just think about that if I have to select I said select if I have to insert I said insert what about update do we have the update yes we have it so now this become very easy because we get the pattern we have so we can simply say update users and here we need to Define what we have to update so let's go on the documentation and here we have the update statement it says update users set field name value and then the where field okay so we say update users set email as ABC at bitfumes.com where ID is equal to okay this is outside like this where ID is equal to 2 okay so let's verify that we have the id2 ID3 actually okay so ID three let's make it ID three Okay so this should also return true if everything is fine like this but we know we will going to get so we are getting one wow that's great now if I comment and fetch and see all the users second user is having ABC at bitfilms.com you will see here that we didn't have to do anything for the bindings but if you want you can do that so suppose you have to do select this question mark and here also you can say question mark as a second parameter you can provide first as the email ID so back with sarthak one and ID is three so let's just log only the response of this statement and it says one very nice so let's comment and fetch all the users and here we see that now it's back with a certain one at bitcoins.com very very nice okay so just we have done with the update a user we need to do similar with the delete a user okay and I can just uh think about this and say that hey I just need to delete see level is that amazing if you know one thing then you can predict what you can do for the next day and that's why the readability and the user or developers easiness is the key point of level so when I said select DB selective then I instantly got that hey if I have to insert it will be DB insert if I have to update it will be DB update and if I have to delete it has to be DB delete so I say delete from users where ID is equal to 3. and all these things will be as a string now obviously if you want you can do the bindings and now let's try this reload and it says one that means I can comment it and get all the users so we should get only one user which is here wow so amazing we saw that just by using this DB facade or select insert update or delete methods we can do all the crud part and this is running raw SQL query but you can see that every time we have to define the user table like here inside this statement and the where condition we have to Define should also be inside this and there is no problem with this but in my opinion this is not that handy and this is not that powerful so that's why laravel has another amazing thing which is called query Builder so once you are done with this once you understand all these things then you can go to the next part of our level database documentation which is query Builder okay now let's do all the this all this crud part with the query Builder and how we can perform the crud
Info
Channel: Bitfumes
Views: 16,842
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: GEj08F8yN_c
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Fri Mar 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.