PHP Laravel Tutorial for Beginners 13 - Update and Delete record from MySQL in Laravel


 hey what's going on guys welcome back to our blog and welcome to this blog so this is the part 13 of our laravel for beginners tutorial and in this blog we're going to discuss or we're going to talk about how to enable editing and deleting of information or deleting of our data coming from our database so in the previous blog we have worked with our post or our blog right so we can now have or we can I will see all the posts coming from our database and of course we can also go into details by clicking the specific post in this particular page so there we go we have here the post 3 and of course we have post two with all the details and now let's try to add the added functionality and also the delete functionality of this particular post so if there are several post here on this particular page so we have an option to edit or even delete that particular post so in order for us to do that we need to go to our code editor and try to modify our show page so as you can see we have the resources the views and of course the page and we also have here the files for our post so we have here - oh that lady at the HP and in here we need to add a link so right after the written iron information so we need to add a link for our edit so let's just try to add furs and HR element to have a separation of these two elements they are small and of course the link that we're going to add so this link should redirect to post then the ID of our post and of course the Edit and then it should say edit and it should have a class VPN and VPN default so if you try to save this and go back to our browser and refresh so we now have the Edit link here and you try to click on this one we are being redirected to this page PK project that LOC slash post and then slash one and then add it so now let's go back to our code editor and try to modify our post controller so up and then HTTP controllers and of course our post controller so we have here the store function and of course we have also the show before or from the previous blog we have worked with show and then we're just getting the post so we're just going to do that also for our edit function and also we can just copy this line and just modify instead of saying show it should have the Edit keyword and of course we're also passing the post variable to our edit page so let's go back to our resources and then post we need to create a new file here to say edit blade that PHP okay and we're going to copy the mark-up from create blade that PHP because I think there are almost the same so in this page or are creating a post and of course in this page we're editing the particular post so instead of saying create a post it should be edit post and of course in here yes we need to say instead of empty we need to say post and of course the title of our post the same with the body of a post so post and body of the post and since we're editing a particular post we need to include or submit also the post ID so instead of just passing the post controller at store so we need to make it an array to pass also the ID of our post so that's post ID there you go and as you can see we have here the post method but if you try to view our route list by opening our terminal so let's try to view first our list so that's the HP artisan and then route list so we have here all the route for our project and as you can see we have here the update right so post and then of course the idea of our post and as you can see the method for this particular route as put or patch but we can't directly say here put so we need to have some kind of mechanism to enable to say put so in here we need to pass in some hidden field so form and then he then and of course the underscore method good so this is somewhat like a bypass because we cannot directly go here and say put so instead it should be post here and then here we can say the method should be put for our update route so save this and then I'm just going to close that terminal and let's go back to our post controller and try to update our update function here so in here in our update function you could just copy the logic coming from our store function so we can copy all this lines and paste it here there you go and we just need to change the post because we're not doing or we're not creating a new post we're editing the post so what we need here is to find the particular post fine and then passing in the ID of the post and of course instead of saying post updated or post created it should be post updated so there you go save our post controller that PHP and let's go back to our browser and in here refresh so we have here the Edit post page so let's try to go back to our blog first and then try to see post two and then this is the post oh and then if you try to click on edit we can see that the title is postal and we can just edit the body here so this is the second post body and this is updated by a certain user just to make sure just to show that we have updated this particular post so submit so we're getting this exception so the put method is not supported for this route so I think we need to change something here so oh there you go so instead of using store it should be update here so save that and let's go back to our page so Doug and then post two and then edit this is the second post buddy and this is updated and then submit so there you go post updated and then if you try to go see the post to we can now see that the post has been updated so now let's add the delete function just like what we have in our edit function or edit form we need to submit also the post ID for our delete function and also specify the move method so you know in that manner we need to also make our delete function as a form so we need to add a form here instead of just a pure link so that's form form and then open and the action of our form that will be post controller and then I destroy because we're using this for particular function to delete our post and then of course the idea of our post post IB specify the post ID and then for this we need to specify the method here so method and of course this should be post and we need to add a class here for our form so class all right because we want this to float right side of our Edit link so pull right and then we're going to close this one close our form and in here we need to add two things the method or the spoofing method that we have the same like for our edit so we need to add hidden field so underscore method and of course the keyword elite and of course here we also need to add the submit so form and then submit open and close and then delete this is our submit button so delete and we need to add a class here for our button so that's BTN and then BTN danger so let's try this refresh so we're getting syntax error and expected period so let's go back to our editor and identify the oh there you go so post it should be the post ID so Save let's go back to our page post three so it says undefined class constant closed so I think we also missed that instead of closed so it should have the open and closed parenthesis so let's go back to our page post three and then delete so now we are being redirected to the destroy function of our post controller so let's go to that function so destroy and in here we just need to find the post just like what we have in our update and show so find the post and then of course we need to delete the post using that function and then we need to redirect to our post page so that's post post and of course we need to pass in a message so that's success and then the post remove so save that and then refresh or maybe it's better to go to our post first so blog and then post three and then press the delete so as you can see we have removed the post number three and we have the message here post remove so we have successfully created our create read update and delete and in the next blog we're going to talk about or we're going to discuss about user authentication so thanks and see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News