hi there how is it going, I need help guys, I will appreciate it.
I am trying to make a full website using laravel 9 and inside it, I am trying to make an admin account in which I can create, edit and delete other user accounts. I was able to delete the users but I still cannot create and edit them. I am a pure beginner and noob in laravel so I may need to be told how to do it step by step and in detail (i looked for the threads here and there and I don’t know if they can help or if they meet my need)
, my routes in web.php:
type here
use AppHttpControllersAdminController;
use AppHttpControllersHomeController;
use IlluminateSupportFacadesRoute;
Route::get('/adduser', [AdminController::class, 'adduser']);
Route::post('/users', [AdminController::class, 'store']);
Route::get('/edituser/{id}', [AdminController::class, 'edituser']);
Route::post('/edituser/{id}', [AdminController::class, 'update']);
Route::get('/deleteuser/{id}', [AdminController::class, 'deleteuser']);
my controller:
type here
public function user()
{
$data = user::all();
return view('admin.users', compact('data'));
}
public function adduser()
{
return view('admin.adduser');
}
public function store(Request $request)
{
$userdata = $request;
$userdata->name = $request->name;
$userdata->password = $request->password;
$userdata->usertype = $request->usertype;
$userdata->save();
return redirect()->back();
}
public function update($id) #this function is for editing the users
{
$data = user::find($id);
$userdata = $request;
$userdata->name = $request->name;
$userdata->password = $request->password;
$userdata->usertype = $request->usertype;
$userdata->save();
return redirect()->back();
}
I didn’t change anything in the default laravel users model, I don’t know if I need to paste it here as well and if there are any other things to paste, please help me guys thank you
I used the default auth of laravel and jetstream as well but didn’t implement many of its features
2
Answers
This is my old code but also made when I was beginner 🙂 Hope it will help you:
Your doing it wrong, you are using the
$request
instead theUser
model.in update model its just like in the store model the difference is that you need the user id.
It is the minimum requirement for CRUD but it is possible to create your own implementation. You can experiment to the methods I commented above.
If you want to learn more about Validation :
Validation in Laravel
More about inserting and updating data in database using model :
Eloquent in Laravel
Hope it helps, Happy Learning.