Laravel Tutorial - 9 Different ways to store data into database using Eloquent Model in Laravel 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our Lal 10 tutorial so now guys in this video we are going to learn about how to save or store the data into our database okay so guys basically we are going to use our eloquent model and store the data in different ways so now guys let's get started so let's move to our editor and continue with our products example so let me just close everything and First Step guys let's move to our routes web. pH P so we have created a route over here you see that we have a product create page which is showing the product form in previous video we have seen that so let me just show you that product SLC create here okay so guys where we have seen about the validation form validation okay so in previous video we have seen that so now guys let us use the same form to save the data okay so let's get back to our editor and let's go to this controller guys over here app HTTP controller there is product controller and add the create method so let's move over there and here is the method which is about the form okay so let me just show you so this is my form guys resource views fronted which is product hyphen create. PHP and here my form starts so this is the card and card body and where is the form tag here let me just show you this is my form tag starts at this container okay so let me just show you the complete form so it's a simple form guys so you can go through this okay so now guys once you click on this submit button it just gives the form submission and the form submit will take you to the action and this action is going to a URL called products SLC create with the method post with the csrf token and once you click the form submit it will take all the input field values okay with the name attribute okay so now guys let us see with this URL with the post method so let's go to web.php and we have already created in previous video because we were doing a form validation tutorial over there okay so now guys uh here you see we are at the store method so let's go there we are already in the product controller let's move below and here is our store method okay or function so in this you see that we have a form validation done here okay so now guys we need to see uh how we can store data in different ways so guys let us see first way to store the data okay so for example we are already working on the products so let me just show you go to apps model and you find that product.php okay so this is my model so let's copy this and get back to your product controller let me just zoom it so now guys let's create one object named product equal to new of your product model okay and you need to import its class so let's import the class okay it's already imported let me just show you so this is my product model class guys okay and now guys with the help of this object we are going to save the data okay so now guys let us call all the fillables so fillable means all your column fields so let me just show you in our database so here we have a products table and inside this product table we have all these fields right so these fields only we have to get it so let me just go back and paste here object of your column name name equal to Dollar request of the name so what is this request request is nothing but the form submitted input request okay so which is coming from this method so so which is getting all the input Fields so let me just show you this is my input field it takes all the values with the help of this name attribute and we are using this name attribute here name description price these are the request we are getting and calling here so we'll call this data and store inside our product model okay so now guys let us copy and paste how much ever you have called for your validation so name description and and let me paste the description so make sure you have the same spelling here okay so we are calling it here and then guys next is price so and then the input field also will be price and then the next input is stock and the column field in the database stock and finally is active so is active is true or false false so we can just assign a value over here like if it is true then make him one else zero so either you can put it single codes or Direct Value like true or false one or zero okay and now guys final step we have to again copy this object and use a save method or function to save the data into your database okay so once it's saved you have to get redirect back so where you want to redirect back on this product create self so there are many ways to redirect guys so you can use redirect function and back function so it will redirect you back to the same page or else you can use directly like I have the route and if you want to send with some messages just add withth function and in that just add a status so which will show you the message added okay so product added so now you can use this status as a session and Print in your blade file so let's move there inside our blade file and after this column md6 I just want to use that so let's use a if condition and use a session function and just check if it is set then you're going to show as a alert message so use div paste that session of status in double CBR to Echo that and just add a class alert alert success that's it guys okay so now you can save it and get back so guys this is the first way we are going to see how to store the record let's get back refresh once and let's submit so let me just show you how many datas we have so here I have three data and now let us try to save with new record it is going to be the fourth record so let me add product 4 here let me paste the same description and just copy this description and paste Here and Now guys just add price talk and check let's click to save okay so product added here you see perfect so we can see that our data is inserted into our products table so now guys this is one way of storing the data let me just comment and keep it and now guys let us see with the second way of storing the data so guys we are going to use the create method of eloquent model so let us see how to use that so product colon colon create method okay and inside this you have to assign array and then inside this all your fbls I mean your table column names and that's it so guys you just need to do the same process whatever you have done here so let me me just use a shortcut contrl D copy everything at the end and paste inside this so here we paste and now guys um let me just again select these few steps and making a single codes and make a arrow and then add at the last comma okay so I have pasted all the fields so guys here one point to be noted which is about the create method so if you're using a create method with this eloquent model then you have to assign the fillable so it's very mandatory so go to your products. PHP model and in that fillable you have to assign all the column field names whatever you want to insert so if you just miss any of the column it's not going to store that record so let me just show you with an example here so right now I'm going to disable the description okay so commented and it's not available now and I'm going to insert This Record let me show you what happens now let's get back refresh once and let's create one new record product 5 and paste here description some random digit number let's click to save so here you see product added but our description is not going to be inserted let me just show you that so browse here you see on our description here you see that it is null we have inserted the product five but description is not inserted we have given the data here but it's not inserted why because we did not give that fillable field here so you have to mention that in your fillable then only you'll be able to insert the record with this create method okay so it's very compulsory to give all the fillable all your table column names inside your eloquent model with this this property fillable okay so now guys let's get back and let's test it again so whether it inserts the record or not so product so paste here description price stock and let me keep unactive and click to save so here you see again product added let's go refresh and here you see that we have added the description cool okay so guys we have done with two ways of inserting record okay so guys let's comment out and let's see on the Third Way of inserting the data so guys again you are going to call your product model and use the same create method to insert the data so guys basically in create method what does it ask is ask for your complete data in Array format as you see we have given here in one complete array okay so all the request whatever you get here okay so this request whatever you get here in what format you get you get in Array format so let me just return and show you so it will be clear and to get all the request all this input field at once you have to use a all function to get all the data okay of that input so now get back let's refresh once and just let me add some dummy details here okay and let's click submit so here you see you are getting in object format because you have returned it but you'll be getting in Array only okay so let me just DD and show you so it will be more clear DD and remove this return okay and now guys let's control and confirm resubmission so here you see that we are getting in Array format so here you see that notice that AR let me zoom in okay so here you see see we are getting an array format cool so reset and let's refresh and now guys you can control X so finally we are getting this dollar request of all in Array format and now you can utilize that here dollar request of all the input field so whatever inputs are available inside your form so it is going to directly come here and insert the data so guys one more thing you have to notice that your request field name okay so which is name attribute name description and price and your product model fillable properties field name should be exactly same so it will understand and then it will store the data okay so now guys let us save and get back once refresh and let's try to save the data so let me just show you how many records we have total six records we have now let's add with new record like product 7 product some description price quantity and let's click to save so here you see product added let's get back browse so here you see that product 7 also is added okay this was the Third Way of saving the record using eloquent model okay so now guys let's continue with another way let me comment and let's create one object product equal to new your product model product model that's it and now instead of using this array in create method we are directly going to use inside this product model itself so whatever function you have opened okay so inside this we are going to pass that array that's it let me uncomment it once done you have to use this object and with the help of save function you have to save the data that's it so now let's get back and let me browse here so we have total seven records let's refresh and insert product 8 product 8 description price stock and click to save so here you see again product added so browse so here you see product eight cool so we have inserted again record with different method okay so guys this way of storing is called injecting your attributes in the model okay and let me comment and let's continue with next way of storing the record which is using fill method so let me just show you let's create uh object again product equal to new product and use this object of fill function and inside this you're going to call all your request data okay so either you can directly use this dollar request of all so let me comment here and either you can use again this fill function of array okay so array means you have to give the same process let me just copy and paste here okay so either you give one by one if you have a different value like the request input name is field is different and your database column field is different so at that time you can use this process or if you have the same exact names then you can use this way and once you fill all this data you need to save it product product of save method so this is the main key guys you have to just save it to save the data let's refresh here add product 9 some description price some quantity and let's save cool let me get back browse and here you see product 9 has been added cool so now guys guys you can just see we have used this way also to save the data and now guys let's see one more example with the query Builder so so query Builder means you can utilize directly DB so DB colon colon use your table name and insert function okay so here you have to mention your table name so my table name is products so let me just copy that table table name which is products get back paste it and guys inside this insert function you have to call your array of data so either you use request of all or call your array of data whatever you have just aligned it according to your request name and your column name okay then guys you need to import this DB so let me import the class okay so here you see that the class is imported so guys if you want this option import class you can just install the PHP name spacer okay let's get back refresh so guys you have to note one thing here like till now whatever we have created the data here you see the created and updated at are created now so you should note that product 10 some description so price quantity save okay and browse here so here you notice that we have got all the data we got all the data and you did not get the created at here okay so created at was not created updated at was not created and also you'll be seeing that slug is also not created so for all the products slug was created and here the slug is not created why because it is not connecting to your eloquent model you are directly specifying that go inside my table and just insert this records so we were using all the product model here like all your model eloquent model and saving the record which was going and checking all your slug let me just show you about the slug part so here you see that when inserting any record we are using this mutator and inserting the slug value okay and also the created and updated also by default it inserts but using this query Builder your directly database and table it doesn't insert guys okay so now guys let me comment and let's see again this same insert function with your elequent model that is your product so let's call our model eloquent model product colon colon insert function so we are going to use the same exact thing here let me paste it so paste here and uncomment okay so we are using that same process guys but we are using with the help of product elequent model and now see the difference so here also it doesn't create your created and updated value let me just show you refresh and let's tell product something 11 or 12 what was the product so we have 10 product let's product 11 description price quantity and click to save okay so browse here and you see that we are not getting the slug also and not even the created and updated at Value also okay so this is the issue with the insert function it just inserts the record but it doesn't affect with our model about mutator or accessor that is your getter and Setter in LL and also the created ATD at timestamp so guys but you can insert the data using this insert function so now let's comment here and now guys let us see with another example which is about finding and inserting the record so let's use the eloquent model that is your product and first FSD first or create function so what about this here so in this we have to pass the two arguments so first will be your condition about checking the data and second all your values so let me just explain you in detail here like let me break it down so it will be more clear okay so first step I'm going to check that whether the product is same or not so let me just copy this paste it and then all the remaining Fields so I can paste that okay now let's try to save and see what happens now here so right now we have product 11 let's insert with product 12 product 12 description price and save so here you see product added let's see inside our database so you see that product 12 has been added cool so now guys let me copy the same product name and let's try to again create that same product name okay so ASD something price your quantity and let's click to save so here you see product added but let's go and see what happens here browse so here you see your record has been not created okay so it just skips that because it's already found so if you want you can just check that in this way like dollar product equal to and which gives some value over here let me just return and show you what it gives the value if record is found or it is not found it is going to create new record okay so yep let's move back and let me copy the same product name here product 12 we are going to copy and paste let me add some description product 99 price and stock let's click save and you see that we are getting that data okay complete data which is already found so it is like 66 and 33 here you find the data price okay so now get back and let's try to insert new record okay and let's save so here you see you got that data perfect so let's see our DB cool so you have got the new data so guys okay so guys we can use this format while inserting some records from the Excel sheet like if the product is already there just ignore it and import the rest of the data so we can use this while Excel import data okay like import data from Excel okay so this was one another format of inserting the data and now guys let us see with another example with the same almost similar to this but there is one simple difference let me just copy the same and paste here and let's uncomment it so now instead of this create we are going to use new so what does this do so the difference is about calling the save method let's just call this save function so here you don't require the save method to save the data because it is using Create and it automatically creates it so if you're using first or new then you have to use this save so guys let us see with an example of this also so yep let's get back and refresh ones let's insert product 13 uh we are already inserted product 13 let's see with product 14 so 14 description price and stock let's click to save so here you see product added and refresh once so you see that product 14 inserted and now let us copy the same product name and insert and just check it out okay so why is this checking only with the product name because we have given in the first argument itself that check with this product name only okay so we are checking with that product name so no confusion and now guys let me insert the prize and stop let's click to save so here you see product added let me browse again so here you see guys that it has not updated any data code so now guys let's continue back and yep so let's comment this format also and let's see with another format one more format about update and create okay so as we have seen first or create so same way we have to see with update or create let me just copy this again and paste P below so un command so now we are going to update the value or create the value okay so with this product name done and now guys let us get back and I'm going to update this product 14 so you see that 14 ESD and 50 and 20 the price are there like price and stock so let's try to update the product name 14 and only a value and I'm going to only 40 and 40 so let's click to save so here you see product added we did not change the message so yep so you see that guys that 14 has been updated value is updated so now let me add product 15 so we are going to add new product product 15 desk so 40 not 40 50 50 and save okay let's refresh and you see that product 15 has been added cool so guys we see the difference of about update or create okay so we can use this while comment system or your Excel upload like existing product should update with the new pricing and different things so you can use it in many ways okay so guys we have have successfully done with the inserting of data into our database in nine ways nine different ways using your elquin model okay so guys in this video that's it and let's continue in next video with new topic related to our larel 10 tutorials guys so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 1,864
Rating: undefined out of 5
Keywords: fundaofwebit, how to save data into database in laravel, save data into database in laravel, laravel 10 tutorail eloquent model, eloquent model tutorial, laravel 10 tutorial query builder, Different ways to store data into database in Laravel, how to save data in different ways using eloquent model, laravel 10 crud application
Id: FahSdy65HV8
Channel Id: undefined
Length: 27min 48sec (1668 seconds)
Published: Thu Dec 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.