Create new user in laravel 5.6
Here are the steps to create a new user in Laravel 5.6:
Method 1: Using the Command Line
You can use the make:user
Artisan command to create a new user:
php artisan make:user
This will create a new user with a default set of attributes (e.g. name, email, password).
Method 2: Using the User Model
You can also create a new user by creating a new instance of the App\User
model and saving it to the database:
use App\User;
$user = new User();
$user->name = 'John Doe';
$user->email = '[email protected]';
$user->password = bcrypt('password'); // use bcrypt to hash the password
$user->save();
Method 3: Using the AuthController
You can also create a new user by using the AuthController
provided by Laravel. You can create a new user by calling the store
method of the AuthController
:
use App\Http\Controllers\Auth\AuthController;
$authController = new AuthController();
$user = $authController->store(['name' => 'John Doe', 'email' => '[email protected]', 'password' => 'password']);
Additional Tips
- Make sure you have the
bcrypt
package installed and configured in your Laravel project. This package is used to hash the password. - You can customize the user creation process by overriding the
create
method in theUser
model or by creating a custom user creation controller. - You can also use the
User::create
method to create a new user, which will automatically hash the password:User::create(['name' => 'John Doe', 'email' => '[email protected]', 'password' => 'password']);