John Davidson

php - Admin create Users in Laravel

0 comments
Message:


I have a admin panel called 'master'. Using this panel, I want to register new users with different roles.


For example, I want to register two new users, named 'John' and 'Alex' with their roles as 'Sales' and 'HR', respectively.


Is it possible in Laravel 5.8, if so, how?


My user registration form in master looks something like this, click here to see the image



My Answer:
Yes, it is possible to create users with different roles in Laravel 5.8. Here's how you can achieve this:

1. Create a User model with a 'role' attribute:
php
php artisan make:model User -m


In the migration file, add a 'role' column to the users table:
php
Schema::table('users', function (Blueprint $table) {
$table->string('role')->nullable();
});


2. Create a UserController with a method to store new users:
php
php artisan make:controller UserController


In the UserController, add a method to store new users with their roles:
php
public function store(Request $request)
{
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->role = $request->role;
$user->save();

return redirect()->back()->with('success', 'User created successfully');
}


3. Create a form in your admin panel to register new users with their roles:
html

@csrf








4. Define a route for the UserController store method:
php
Route::post('/users', 'UserController@store')->name('users.store');


Now, you can use the form in your admin panel to register new users with their roles. The UserController will handle the creation of users with the specified roles.

Rate this post

4 of 5 based on 5385 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us