Skip to content
Advertisement

Edit data from database using forms laravel

So I was planning to do a type of form to edit the user’s data. I am currently using laravel-8 and I am quite new to it. How do I edit the the data using forms and request can someone help out? I was planning to put the function in my Dashboard controller and the form to a blank page I will be working on. Here is the controller so far:

JavaScript

I wanted to put it in the profile function. Any help would be greatly appreciated 😀

Advertisement

Answer

I remember making the same mistake when learning laravel lol 🙂

Nope, what you need to do is create a User Resource Controller and let Laravel do the job for you using the following artisan command:

JavaScript

Or if you want to create controller, migration, model, factory and seeder on one command just do (be careful because laravel already has a user model):

JavaScript

Laravel will create a Resource Controller in there you will have predefined functions that you can use to update all the required data

You also need to update your routes file to add your resource controller like this:

JavaScript

Laravel will automatically create all the routes to use the controller for instance if you go to localhost/yourapp/public/user Laravel will invoke the index function, in there you just send all your start data maybe like this:

JavaScript

if you go to localhost/yourapp/public/user/create Laravel will invoke the create method of the user class, if you just want to retreive a specific user you just need to make a get request to localhost/yourapp/public/user/{user_id} and in the show function put something like this:

JavaScript

To update a user using a post request you just make a post request to localhost/yourapp/public/user/{user_id}, and in the controller have something like this:

JavaScript

I can go all the day but its best if you try to find and follow many laravel CRUD tutorials that show you the code and go explaining step by step or read the documentation:

https://laravel.com/docs/8.x/controllers
https://codingdriver.com/laravel-8-crud-app-example-tutorial.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement