Laravel 8 Form Request Validation CRUD 3: Update data with Unique Form Request Validation in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our larval form request validation so now guys this is part three and where we are going to see how to edit and update the data with the same form request validation okay so we will be seeing about the unique validation in level 8 so yep guys as you can see that while adding when we save data here you find all the data so now let me add one existing email over here at gmail.com so okay so this is an already added email so you'll find the email already exist so same form we are going to use and same form request validation will be using to update also okay so at that time it should not show this error so let's begin with it okay so let's go to this edit button here we are and we are using the resource route guys okay route resource so how will you find that edit button option so you can just type the route list php artisan route list and here you will get your data and for editing the data you can see that edit option is given over here and this is the path how you can edit it okay so let's copy this and call that in our edit button so students forward slash your id and then the edit method okay so let's concatenate and call the dollar item of id let me just divide this set so let's get back refresh and now let's click on this edit so i'm going to edit on id2 edit so you are getting the perfect path so now guys let's get the data with the help of model let's go to our controller at edit so yep this is our edit method and here you can see this you need to call the model student model you have to call and then let's return this so i'm not using any id so you can get the output perfect so you are getting id number two if you search with id number one here you'll get your id number one data go on with three you are getting three if you go on with a wrong id you will find null value okay so i mean four not four so let's continue with rd2 and now let's get this data in our blade file let me just comment this a return view i want to go to my student folder at edit page okay so we don't have an edit page now let's create that inside my student edit dot blade dot php done and we required the same as create form so let's copy all the code and paste for our edit okay so let's see the output refresh so here we get let's change the title instead of add we will tell edit student done guys so now you can change the button name as update student let me just minimize this okay so now guys let's fetch the data inside this input fields so let's get back and yep so when you're passing this in your edit right when you're calling the edit function you're calling the page inside this only let's use compact function and inside this compact function you are passing this dollar student data done and with this help of student data you are going to your edit page and get the value inside the value attribute of input field dollar student office database column field name so that is full name so let me just show you that database column field name full name email phone and course so whatever you have this if you're using a model okay if you're using a model over here so make sure you are specifying all the fields over here or else it might misbehave the data okay so yep let's copy this and paste here for the second third and fourth quotes phone next was email done so you can check the output perfect so here you get for the home let's move on with id1 here you add edit id1 get the fade okay so let's get back to id number two itself done and now guys let's submit this form so when you update the form you are going to have update the url so let's check the route list so this is our route list for resource route and to update the data you are going with student and then just send the id okay so let's send that this is my students forward slash concatenate dollar student of id that's it so your student variable from your controller and getting your id send that with the post method where you have to send this method with the help of while updating your setting put or patch you have to use it okay so let's use that also at method where we will tell put that's it so when you call this put it will go to your resource and around it will take and go to your student controller so add this update function so now guys while updating it we need to check the form request validation so as we have done for our saving storing the student reform request right so same form request we are going to copy that so this is our form request let's add that in our update function okay so whatever request you get in the form request and this is our model so yep guys and now let's create a variable data is equal to your requested should be validated okay so all the requests should be validated and then only you can store that data into our database so it's now over here let's use the model we are not going to search the id and all so let's use the model itself where i will tell phil this dollar data so whatever data comes let's save that save function so once saved you can just return and redirect to the web page i mean main page so let me just copy that and paste and tell updated successfully and now guys let's check this output so we have let me just update the data on prakash update okay please enter the phone number email already exist it's giving right okay so here you see please enter phone number i think data has come wrong let's check that first at edit blade here you need to change that phone okay we have missed that i mean i have just did a mistake over here refresh okay so phone is coming now let's update this so you find student updated successfully okay where nothing missed let's get back and if you try to add this veda gmail.com to ohm let's update here you find student updated but did not update the email but we need to do the validation so let's get back to our controller here at student form request let's go to this student form request at rules we have set the global right over here to validate this but in post method we validated the email but what in case of put or patch method so in that case you are going to check this one okay i'm using put method so i'm adding that put and over here you need to write a rule for it so now guys let's test with this also once let me just show you so i'm going to update this over here you can see email already been taken okay so let's write the code so let us use a rule over here rule colon colon unique unique function okay so inside this unique function our students table we are going to check done and we have to ignore while update so ignore function and inside this you can use dollar this of students student okay so this student means your model it will check your model and update it so now let's refresh and let's try to again update it so you can see let me just add one pra update okay let's see rule not found let's import that class okay import class illuminate validate rule okay so you need to just add this rule done and let's get back guys refresh let's update one second update perfect so student updated here you can see that on prakash and email is same so now guys i am going to edit on prakash and add the veda gmail.com let's check that editing with id2 and i'm adding adding as we gmail.com update so you can find email has already been taken done and now let's try to update with existing record itself perfect so student updated successfully so that this was one method of updating the unique value okay with form validation so we are using the same form request to update insert and update the data done so i am going to teach you one more method guys instead of this student direct model you can call the another method dollar this of route okay and what route you are going to call your resource route so your model student okay done and this will be your let's keep that in a variable student copy this let me just copy and paste let's comment this for the reference and let's take this variable student of id so that column id it will take and ignore that so now let's check this method also so let's edit with id number one and give the full name and update student here we go student updated successfully let's try to mix with email home gmail.com an existing email so it should throw an error email has already been taken perfect so guys we have successfully done with our how to do update the data while using the form request validation for the same okay so same form request validation is used for inserting and updating the data so guys thank you for watching this video and please continue next video with that delete part using your resource route so thank you once again guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 8,308
Rating: undefined out of 5
Keywords: fundaofwebit, form request validation while update, form request validation laravel 8, update data with unique form request validation in laravel, insert data in laravel form request validation, insert and update data with form request validation in laravel 8, laravel crud form request validation, laravel validation in update ignore unique data in laravel, how to update data with same form request while insert in laravel, laravel form request validation tutorial
Id: RIJSDNBznb8
Channel Id: undefined
Length: 14min 36sec (876 seconds)
Published: Fri Dec 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.