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:
<?php namespace AppHttpControllers; use AppModelsUser; use Auth; use IlluminateHttpRequest; class DashboardController extends Controller { public function dashboard() { $dashboardTitle = "Dashboard"; $isCurrent = "dashboard"; return view('dashboard.index', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent ] ); } public function profile() { $dashboardTitle = "Profile"; $isCurrent = "Profile"; return view('dashboard.profile', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent ] ); } public function directory() { $dashboardTitle = "Directory"; $isCurrent = "Directory"; $users = User::all(); return view('dashboard.directory', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent, 'users' => $users ]); } public function journal() { $dashboardTitle = "Journal"; $isCurrent = "Journal"; return view('dashboard.journal', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent ] ); } public function files() { $dashboardTitle = "Files"; $isCurrent = "Files"; return view('dashboard.files', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent ] ); } }
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:
php artisan make:controller UserController -r -m User
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):
php artisan make:model User -a
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:
Route::resource('user', UserController::class);
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:
public function index() { $dashboardTitle = "Dashboard"; $isCurrent = "dashboard"; return view('dashboard.index', [ 'dashboardTitle' => $dashboardTitle, 'isCurrent' => $isCurrent ] );
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:
public function show($id) { return view('user', [ 'user' => User::find($id), ]); }
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:
public function update(Request $request, User $user) { $user->fill($request->all()); $user->save(); return response()->json([ 'success' => 'ok', 'msg' => 'User modified', ], 200); }
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