Laravel 10 - CRUD Operation Tutorial for beginners step by step | Complete Laravel 10 CRUD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn about laral 10 crud operation so where we'll be seeing about insert fetch edit and update and delete the record okay so now guys let's get started so guys to begin with uh larel you have to just install your larl application firstly and so guys I have just installed my larl and I have served the application so if you don't know to install and serve the application just go through my playlist of larel 10 tutorial where about the installation and if you want to learn more about login and registration creating layouts blade components and many more things okay in detail about migration files alquin model and form handling okay so now guys let's get started so first step guys we need to connect our database so just go to your EnV file and here is your DB connection the database name and username root and password is null okay so guys our database is set up and make sure you have created in your admin dashboard so I have already created guys and now let's get back and First Step guys we are going to start with creating the migration file so we will take an example of creating a category table okay so let me just create a new terminal I me open new terminal and create the migration using PHP artison command so PHP Artis make colon migration create categories table okay hit enter so it will create a migration file with this name categories table okay so now guys you can find that in database migration and at the last you'll find that table name as create categories table cool okay so now guys let us add the category Fields so I will add as table the string which is going to be a Vare and category name will be name and then description and one more thing if you want to add you can add the is active part which is going to be a Boolean type so Boolean where is active Okay and by default function of one and now guys let's push this table into our database so for that you can just enter this command PHP Artis migrate hit enter so it migrates the table perfect it's migrated and now you can just find that categories inside this databases so here tables okay so we find this categories table perfect so our table is created guys with this three column fields and other three fields are uh time stamp and the ID okay so now guys let's close this migration file and let's create one model so PHP artisan make colon model where the model name is going to be category category hit enter so your model is created so guys this model is nothing but it's about uh connecting your table okay to this laral application so let's close all and go to your app models where category.php perfect so you are here guys so now guys let's call the property called table so before that just call the protected dollar table equal to single codes and your table name so we have already the table name you can just go inser data migration and your migration file and you can find that at screa create that is your categories table name so now close it paste here done guys and now let's again call all the fillables so fillable is basically nothing but are the table column field names okay so what are those column Fields like you see here name description and is active and this primary key ID and created updated ad are not mandatory okay so you can give whatever you have created the column Fields so let me just paste here and let's put them in single codes and separate them by comma done so guys our model is set up and now you can close this model and let's create One controller so PHP Artis make colon controller where it's going to be a category controller so category controller hit enter so our controller is also created you can just find that in app HTTP controller where category controller perfect so our controller is created guys and now guys let's begin with our form design and form integration part so let's close everything and go to your routes first and move to web.php so now guys uh you need to create a route so let me create a route colon get method which is going to be the categories so this will be our path and then your controller name so let us call the path of that controller app back slash HTTP back slash controller back slash controller name so it's category controller then colon colon class and give comma and then in single codes make sure you are going to the function so now you need to go to this category controller let's move by clicking on it and create one function so public function where the function name indexed open and close calib so guys let me just test it so return here funa let's save and now you can just copy this route and go to your browser just call that URL perfect so you can see see that funa has been called okay so now guys let us create the form and call so yep let's move back to our controller and let's remove the string and call our view function so inside this view function you will call the path of your files so let me just show you that where you are going to create just go inside the resource views and inside views you'll be creating your files okay so let's create one folder called category hit enter and inside this category you'll be creating your files so let me create a file named index. blade. PHP so we have created it and now let us just hello category and now guys you need to call that file path just call your category folder dot your file index blade file okay so you reach that cool and now guys if you refresh you'll be able to see that file details here done so now guys let's begin with the designing part here so guys let's extend our layout so I have already created one layout guys that is app web layout which I have linked the bootstrap CSS over here okay so everything is set up here so if you want to learn how to set up all these you can just watch out these videos how to create your layout in component and set up a Navar so this is the output it's going to be okay so now guys let's get back and use this layout in our index blade so yep so for that you can just use this x hyphen your app hyphen web hyphen layout and close the file done and now guys let's see the output for this refresh perfect so guys let me just design this complete form as a table view so user container row column md12 and a simple card card header where a H4 tag mentioning about categories and here create a a tag for add category and then guys use a class to design this button with the primary and Float to end okay after this card header I want to design a card body so we can list all the categories Here and Now guys we have a title option in this app web layout so you can see that your variable we have used just copy this variable and use that so use a x slot slot open and close open and close and add a name attribute name equal to your variable name title and then you can just add add the title as categories so now guys let's see the output for this get back refresh perfect so you see the title also and your details let me just give a space from Top margin top four five refresh done so now guys let's uh create the form for inserting the record so for that just open and close cbes call the URL and mention that I want to use the same URL categories forward SLC create okay so we are going to copy this URL and create a route again create a get method route oops so change that and paste and then use a same controller to create the route so let me just copy this and paste here so we have pasted and now guys change the function name copy this create the function name and create a function with the same function name that is create and then guys you can again use the same view to create the create blade file so let's go to the resource views and inside this category we have the file name as create. blade. PHP okay and then guys again same layout extending and title you can copy this everything same and paste inside that so we pasted let me just change the title add category change the heading also and change this as a back button and just change the route as categories only and inside this card body you can start your form design so what is our fields we have already set let me just show you inside my model so app model category.php so we have three Fields name description and is active so get back to your create blade file and now guys let's start with the form tag open and close form and let me minimize this sidebar use a margin bottom three and the input so before input you need a l table like name and you can remove this for add a name attribute called name and value I want the old value so use a old function of name so what is this exact name what you give here you have to paste it okay so guys let's copy this and paste again so for the description let me use a text area so description as a label and text area name attribute description and let's use a class here and design this class as form control and how many rows I want only three rows and whatever value comes you can just paste inside this text area okay open and close let me copy and paste this so guys let me copy this class and paste for this input also so it will look good margin bottom three again for the checkbox so you can copy this paste here and input type text instead of text it's going to be checkbox okay so change this to is active and name attribute value also is active Okay and let's remove this class and instead of value you have to to directly print it so here we will be mentioning that if double equal to true then mention it is checked else null that's it you don't have to do anything and now guys let's create a submit button save or submit and this button type it's going to be a submit and let's design this button with the help of bootstrap classes button button primary and now guys once you click save button it's take all the input field names and goes to the form action with the method post and it also accepts a csrf token so in larel the csrf is mandatory okay so guys once the save button is clicked you are going to an action so where does it go so let's give a route to go to a specific route okay so you can use a URL method to call the route so what I want to do I want to use the same URL what we are going to it let me copy and paste here where is our create blade okay paste it and now let me show you the output of it refresh wantes click add category so here category for/ create and here is your form cool let's go back to our web.php and create a route colon colon post method and same route okay exact same route and then guys we need to go to a controller where we can save the data so let's go to the same controller that is our category controller and go to different function so we are going to create a store function so let's move to our category controller I think we are missing something okay here is typo double colon you have to give and then it works fine so get back to your controller again let's create a function the function name is store and once if it is coming from the post request you are getting a request right so you have to call the request dollar request okay now you get all the input field values inside this dollar variable request and now guys let us validate all the input Fields so guys there are many ways to validate the input Fields let me just show you here you can go to this um form handling and validation tutorial so I have thought many ways of validation in larel okay so yep guys let me just do a quick validation here dollar request of validate function oops validate function and array so inside this array just create your name that is input name okay request of name which is coming from your name then and description and is active so name which is going to be a required field and then it's Max 255 what is this Max 255 it is length of that let me just show you in the database structure here you see that name Vare of 255 it is given so description also is 255 so you can just validate according to it and then guys use the description Arrow again you can same use the validation part so yep and it is going to be a string so here also you can add it is going to be a string and the next is is active just give comma here is active is active is about true and false so sometimes you'll give the check else it is going to be so let's add only sometimes here so it will accept Okay and now guys let us use our category model to save the data let's use the category model colon colon create method okay so import this category class guys first so here it's imported and this is the app model and then category so that is the category model okay let me just show you here so this is my app model that is C PHP so we are importing it and also if you want to know how this import class has come just install the extension about PHP namespace okay yep and now guys use this create method to insert the data so First Column name inside your table column name and your request of name your input name okay let's copy and paste so this is my description and this is the column name inside the category model so let me just show you this is very important guys you have to give this if you don't give this it is not going to save because you are using a create method here okay so if you're using in this way to save the data you have to give this fillable so don't forget to give this fillable values let's get back to controller and continue with the is active so is active is double equal to True question mark here and accept one else zero which is true or false so let me copy and paste the column name is active done so once it is created let's tell return and redirect back so you can use either back function to redirect or you can specify the route where you want to redirect back so I just want to redirect here on this route itself so I know the path I give it directly and I I also want to show the message that it is created so us a status and add the category created okay and now guys let's copy this and just go to this uh redirect back where it's going it's going on create file so I can just go back again and show the success message at here let me use uh if condition and use a session function and Status so it will just display the data let me use a div div open and close crace and print that and make this div as alert success okay and now guys coming to this validation what about this validation details so you can just copy each of the field name and and just show below them use at error of name so this is description let me show a name and use a span tag to design the text here text danger and open and close cies add a dollar message that's it guys now copy this paste again and just change the error of name that is description and coming to is active just change this to is active done now you can get back to your controller and everything is set so you can get back refresh and let's click to save without field so here you see name field is required description is required cool so now guys let me add a category like a mobile so mobile description and let's click to to save so category added you got the message let's go to our database browse and here you see we have inserted the record perfect so now guys let me insert one more category like uh laptop so laptop description and let me make it to is active save and you see that we have one value also okay perfect everything is set guys and now let's go back to our categories so we are here and now we are going to Loop these details into this categories page so get back so first step uh we'll go to the web.php which is on this categories let's go to routes web.php and here is our get method of categories it goes to the index function let's move to that controller at index function so we are here on this page we have to pass the data so guys let's create one variable as categories equal to get your category model elequent model and use a get method to get all the data so guys you get all the details here let me just return and show you so instead of form it is going to return this variable details let's refresh now here you see we have two records awesome so now guys let us use this variable to push inside this form so you can use a compact function without dollar symbol okay just use the same variable name so you can access inside this file so let's go to this file category index blade. PHP and we have a card body here so inside this card body I'm just going to directly press that variable dollar categories and now you'll be able to see all the data in one array so now let me minimize and just create a table format guys so let's begin with the HTML table here and bootstrap classes table table bordered as typo here table striped and then table head table row table heading T head first is ID then name we have a description description and then is active and finally one action button for edit and delete okay and then guys use a table body that is T body oops I missed the table here okay and now let's user for each so you can utilize this variable categories here that's control X and these are the items and now use a table row so each row it's going to loop table data the first value dollar item of ID so why ID because we are getting the ID here and then name then description and then is active and then finally one TD for edit and delete so let's make it edit and delete and now guys let's see the output for it so yep get back refresh cool so we see that we have fetched the data so now guys instead of showing zero and one you can use another way so just PR break this down and use a if condition and else so is active is about true and false so if it is true it is going to show active else inactive okay that's it now refresh done so now guys we have done with the adding the data and we have listed the data and now let's begin with the edit and delete the data so first we'll start with edit so let's create a route here first on this edit button let me Design This edit button guys using bootstrap class of Button as success MX2 and in this href let's create a URL with the same route format which is going to be the categories so copy that paste here forward slash then ID and forward slash and then edit so in between you have to concatenate your dollar item of ID so let's copy this and paste here so we past it done and now guys you can get back refresh so let's click on this edit button here you see we found a URL categories then ID and then edit okay so let's copy this and get back to your web. PHP so let's create a route using get method to call the form on this edit so let's paste here and instad of one you are going to get Dynamic data so you can mention that as ID over here and then guys you're are going to Target at One controller so let me copy the same controller path here let's copy and paste and you need to change the function name edit so now you can copy this edit and go to your category controller and create one new function as public function the function name edit open and close C so guys first step when you're coming to this route you are getting a parameter value that is ID so you need to get that int of ID and with this help of ID you are going to get the category so let's create one category variable equal to call your model category colon colon use find function to find the data okay so guys there are many ways to get the data like find and find or fail so this has different things to do like if you use find or fail if you don't find the ID it is going to show 4 not4 or you can use a wear condition or we ID you have many ways to do so I prefer doing find or fail like if the ID is found just show the data else just show 4 not4 so I'll be showing you that let's use dollar ID here so if we find the data then it's going to return the exact value let me just return and show you the value okay so yep let's get back now if you refresh you have the ID one inside your database so it's showing the ID 1 and we don't have ID3 so let me just give here it shows 44 not F if you find for id2 you have the data cool so this is how our find or or fail works okay so if you just use this find function and you do that here for ID3 it's going to give an error okay so here you don't have any data it's showing just a white page and if you just try to access this in your view file okay in your blade file then you might get an error like you are trying to access some variable without any data okay so guys uh let's continue with showing in the blade file so yep let's comment this and just return View and you need to go to your category folder and create one edit blade file so let's go to our resource resource views and category so inside the category you have to create a edit blade file so guys we are not going to create a new file instead we'll copy this create blade and copy and paste and now guys let's rename this file so just press FS2 and rename the file edit blade. PHP hit enter so guys our file is created and now let's change the titles according to it so instead of add let's make it to edit so here edit category and then guys let us first get the data inside the input Fields so for that uh let's move to our controller and this is the variable you have have to pass inside your view edit file okay so let's use a compact function to pass that variable okay so we have got the variable here category and get to your edit and inside the each input field you have a value attribute so inside that value let's remove this old and paste that variable category of category of name okay so you have to use like this let me just remove for the textt area and add the description and for the checkbox also so here we are checking with the other data hold and instead of that category of is active that's it guys and now change the text here as update and let's see the output first and then we'll see about submitting the data refresh cool so you see that we have got the details here and now guys I will try to access the ID number three which is not there inside our record so here you see you're getting a error like attempt to read a property name on null so we don't have ID3 value so we are accessing that variable here so it's not able to read so this was the error if you use find so instead of find I use find or fail so which it returns directly 44 okay refresh it just gives 44 and if you have the correct value add one it gives the value cool so now guys let's get back and let's work on submit of update go to your edit blade. PHP let me close this so it will be more spacious okay so once you click on this update button it goes to the form action so let us just copy this dollar category of name and concatenate here like Dot and pass that ID so category of ID so let's change the route also guys let's remove this create and just go add the end Dot and single codes use forward slash and edit and now guys you need to use this form submit using a put method so once you submit this form this method should be accepted as put so you can use a option called at method which is I'm accepting a route as put okay so let's copy this uh path guys and the route is going to be put let's go to our web.php create a route and its method is put okay and paste the route so here you are passing a dynamic ID let's remove this concatenation and variables and just tell yeah Dynamic ad is going to come and then send your uh to your controller so let me copy from above and paste here and let's make it to update so guys let's copy this and go to your category controller and create one new function named as update so yep let's create one function public function function name is update and open and close Cate we will take all the request dollar request and then your ID so ID and which is coming in an INT format so now guys let's do a validation and update the record so let me copy from above so we can save time let's copy everything and paste here on the update method so first step you're doing the validation so no changes in the validation and coming to this category of create so instead of create you are going to use update okay so before update you have to tell like what column you're going to update so you can use again the same format find or fail method and then update that record so now what is the find or fail value it's going to be ID so here you'll tell like just find this ID and then update that particular records and then you can return and redirect back to which page uh you can just red to the same page so just use this back function so from wherever you submit the form it just redirects back there only so don't have to worry about this just let's test it refresh so I'm going to update the mobile description as um let's add some dummy record here and click to update so here you see category updated and the value is also updated let me just show you here in description cool so guys our category is updated and let's check here and update so okay identify constant as checked so we just need to give in a single Cod guys I have just missed that so it's not able to understand what it is so you can just get back and go to your edit blade and just mention that in a single codes that's it now get back refresh everything works fine so here you see it's zero Let me refresh it is going to be one cool so by default it is checked okay so now guys we have done with the edit and update the data and now let's get back and start with the delete option so yep let's move back to our index blade. PHP where is our delete button so now guys let me just design this delete button button button danger and mx1 so refresh perfect looks cool and now guys you need to create a URL for the deleting the data okay so guys you have many methods to delete the record like you can use a delete method to delete the data or get method to delete the data so right now I'm just going to teach you using a get method itself let me copy this and paste here and just change this route to delete okay so using this ID I'm going to delete the record so let's copy this route URL and create one new route you using get method so route colon colon get semicolon and paste your route so here we are going to get the dynamic IDs so mention that ID and then go to a particular function at a controller so let's target to that controller at a destroy function so let's copy this and create a destroy function so create a function public function the function name name destroy open and close craies so on that click we are just getting a ID parameter okay so just get that like int of dollar ID I'm getting and then category model colon colon just find the data or you can use find or fail function first you find the record if it is found then you're going to delete that so let's use dollar category and with this help of category use a delete function okay so this is the main key guys to delete the record once deleted you can redirect back okay so your util category deleted that's it and now guys let's get back and refresh so let me add one dummy record as let's see save okay go back so here you see our categor is added and now let's click to delete so click here delete perfect it deleted guys okay so you have no record and if you want to confirm and delete you can just do that option Also let's go to that index blade file and just add a simple JavaScript here let me break it down so it will be more clear to understand so this is my a tag and here okay and inside this just add on click return return confirm function just add are you sure question mark that's it and refresh so let me add one D Cod submit go back let's click on delete so here you see are you sure it is asking cancel it's not going to delete if you say okay then it will delete that record perfect so guys we have successfully seen about how to create a k application in LEL 10 okay 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: 27,457
Rating: undefined out of 5
Keywords: fundaofwebit, how to make a crud operation in laravel 10, laravel 10 crud tutorial, laravel crud using blade component, laravel 10 crud for beginners, laravel 10 tutorial crud step by step, CRUD operation in laravel 10, complete laravel crud tutorial, how to insert data in laravel, how to create read edit update delete data in laravel, laravel 10 updates for crud, laravel 10 crud application, laravel 10 crud step by step, laravel 10 crud, laravel tutorial for beginners
Id: w6vMy1dSF-M
Channel Id: undefined
Length: 39min 11sec (2351 seconds)
Published: Fri Dec 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.