Laravel 10 Create and Update Form | Use the same form for Create and Update using MySQL database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends if you want to create an update using forms in laravel you can use a single form for both add and edit you don't need to create two separate forms I'll show you how you can add and edit product categories using a single form or a single blade file by creating an entire application using LEL 10 okay we'll create a project in lar 10 so I have a larel folder I'll go to Common prompt and here let us create a project we'll give the project name as Lara form so we are creating a project in l 10 and our project name is lore form so it will take some time while the project is being created let us create a database for the project so we'll create a new database we'll give the database name as Lara form so database is created so our project is created now so you can see there is a folder lore form created under LEL directory let us open this project in vs code let us run PHP artion sub so if we run it now okay the project is running fine we'll update the environment file for database name we'll change the database name to Lara form let us go to the terminal so we'll have a new table called categories we'll create the category model along with the migration so minus same option will create the migration file also okay so the model is created app models category.php and we have that database migration file also created create categories table we will have two fields for this categories table we'll add name and description for the category let us run the migration so the migration is completed let us just check the database so that tables have been created this is the categories table and if you see the structure name and description have been added for this table we'll now create a resource controller so we are creating category controller with minus minus resource option which will create the standard methods for the croud operation so the controller is created so this is the controller where we have the standard methods index create store show edit update and Destroy we'll add a resource route also so let us update the route so we'll have the standard routes available for this uh category route these are the routes we can use for this application categories. index create store and edit and update let us now update the controller let us close this for now so in the index function we'll just select everything from the categories table and we'll call a index view to display all the categories in a HTML table so we are selecting from the category table order by name then we are calling the index view so we'll have to create the views now so let us go to the Views folder so first we'll create the layouts we'll create header layout Master layout and then we'll create index View so we'll get the links for the boep file so we'll take the Boost five copy it and then paste it here okay now let us change the title we'll also add a link for our custom Styles sheet so we'll create a CSS folder under that we'll have the style. CSS so let me just add it here we'll just keep the head section we remove this now we'll create a master layout so this is our Master layout we'll use it for the other views so let us now create the index view so in the index view we'll display all the categories in a HTML table so let us create a HTML table so for the table we will have uh name and description along with that we'll displace real number and we'll also add a action field so in a loop we will display the categories from the index method we have already selected the data we will use it here for the action we will keep it blank as of now we'll add it later in case there is no data we'll just display no data found let us just use a column fan so we are displaying all the categories in a HTML table here we'll add the edit action later so our index view is created now let us just run it and see we have to see our routes here so for index we have the categories U so we use it here so we'll use it/ categories okay so it is displayed we will add a few Styles here so for CSS we have to if you see our header we have added CSS style. CSS so we'll create a CSS folder we'll give some background color and color for the header and we'll align them in the middle we'll reduce the width also we'll just add a heading okay so now let us just add one row and see how the data is displayed in the table so we just manually create one row so let us now add a button here on the right side to add the new category so since we are adding the category so we'll have the route so we'll have the create route categories. create okay so we'll just put it on the right side let us add a few classes here okay so now so we have to now write code for the create method in controller so now we'll create a new view add category so so we'll create add form here we display the validation error here let us just copy this for the description also so we'll add a submit button also okay now so for the add form we'll have the route categories. store this is our ad form let us just add a heading here now we need to add code for the store method in controller so we have to valid the name and description and then we'll insert a row in the category model and in case of error we'll display the message we'll display validation message also user friendly message we use strike as here so we are creating a row in category model so we are using name and description and then once it is inserted successfully we'll display message and redirect the user to the index page so for index we have the route categories do index so we use this route so in case of error we'll just stay in the same page so we'll just use return back okay so let us now run and see so we repress it add category okay so the form is displayed we just use some style here okay so let us just click on submit so page expired because we have not added the csrf token so we just add it here okay it is showing the message so let us add CSS here let us try to submit the form with data looks like it is not adding anything so we have some problem here if you see the categories table we have just test data only the reason is that we have not updated the model and we need to update the model for the Fel columns so here name and description of the F columns we just update it here still it is not working let us see so data is not inserted here it should be a comma here so let us just run it now okay data has been inserted we just need to display the message if you go to category controller we are redirecting the user to the index view so we need to display the message in index View and in case of error it stays here so we'll have to add the error message in add category form so let us do that so here we'll add the message so we'll add an alert here success alert it will be error here some error is here okay so the message is displayed here and the data is successfully added so now we'll add edit button here to update the category so here we'll add so for the edit we have the route uh for edit let us see so we have categories. edit we have to give the parameter here for the category ID so now we have to go to the controller or edit method so here in this case we are not creating a separate uh View for the edit so add category form only we will be using so we are fing the data from the category for that ID and then calling the add category form only same name form so if we just run it now let us see click on edit so you can see it is showing the ad product category only so we need to change this form so that we can use it for update also so first of all we have to change the header so we go to add category so in case of edit we will have the category ID available so we'll use that so basically we are checking in case in case ID is available we'll use this form as update otherwise we'll use it we'll use it as ADD product category so if you run it now so it is showing update product category now let us see the form actions we have to change the form action also so in case of update we have categories do update so we'll use this route so we'll use the same eils here so this will be our update and this will be stored for update we have to use the method put so we'll use this so the route is changed now now we have to change the value also since this is update form so we have to display the existing values also so we'll change the value attribute also here so here also we'll use eils so we are checking in case of category ID is available so we'll display the name otherwise blank similarly for description also we'll just copy it okay so our update form is done so let us just try to update an existing category so there is some problem here okay so for update we have to give the ID also so here if you go to the action so this is our categories update we need to give the ID also this is our form so now we have to write the controller code for update so we just copy the code from the store and paste it in the update method same validation will be applicable here also so instead of create we'll use update and before that we have to find the data so we'll use a high condition so let us try to update an existing category let us add a cancel button also here so in case of cancel we'll just we'll use the index route so categories do index let us update this okay so it is successfully updated you can see so our add and update form is working so let us just try another one update okay so it's working let us add another category so it is correctly updating so this way we can use the same form for ADD and update so we are using the same view only one view only add category to add the category as well as update the category so this was a simple application and we can use the same form for ADD and update hope you could understand this application how it was developed you can visit the website Cod out.com for a detail stepbystep development guide for this application you can download the source code you don't need to register yourself please see the description of this video for all the links friends if this video was useful to you please give a like And subscribe to my channel thank you
Info
Channel: Codehow2
Views: 779
Rating: undefined out of 5
Keywords: Laravel, Laravel add form, Laravel edit form, resource controller, resource routes, laravel add and edit, php development, php web development, php tutorial, php course, php developer, mysql, html, css, learn-php, laravel create update, laravel tutorial, Laravel 10 for beginner, laravel 10, laravel full course, crud laravel 10, laravel installation in windows 11, laravel project, laravel 10 full course, laravel eloquent, laravel crud, laravel
Id: 391JLjt2rCg
Channel Id: undefined
Length: 28min 42sec (1722 seconds)
Published: Wed Nov 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.