Create role based authentication in Laravel

01.Create a laravel 5.4 projects
composer create-project laravel/laravel AuthenticationLaravel “5.4.*”
We can use larave 5.5 which is LTS version but requires PHP 7.1.3 .If client machine did nit match criteria then go for previous version

02. Create auth
php artisan make:auth

03.Run migration .(php artisan list @for listing all available commands)
php artisan migration:install

04. update routes/web.php

$this->get(‘login’, ‘Auth\LoginController@showLoginForm’)->name(‘login’);
$this->post(‘login’, ‘Auth\LoginController@login’);
$this->post(‘logout’, ‘Auth\LoginController@logout’)->name(‘logout’);

// Registration Routes…
$this->get(‘register’, ‘Auth\RegisterController@showRegistrationForm’)->name(‘register’);
$this->post(‘register’, ‘Auth\RegisterController@register’);
// Password Reset Routes…
$this->get(‘password/reset’, ‘Auth\ForgotPasswordController@showLinkRequestForm’)->name(‘password.request’);
$this->post(‘password/email’, ‘Auth\ForgotPasswordController@sendResetLinkEmail’)->name(‘password.email’);
$this->get(‘password/reset/{token}’, ‘Auth\ResetPasswordController@showResetForm’)->name(‘password.reset’);
$this->post(‘password/reset’, ‘Auth\ResetPasswordController@reset’);

05. for adding a column in register form
i. php artisan make:migration add_extra_field_to_users_table –table=users
ii. In migration file
public function up()
{
Schema::table(‘users’, function (Blueprint $table) {
$table->string(‘test_field’);
});
}
public function down()
{
Schema::table(‘users’, function (Blueprint $table) {
$table->dropColumn(‘test_field’);
});
}

iii. php artisan migrate
iv. in \resources\views\auth\register.blade.php

v. Add validation in Register controller
protected function validator(array $data)
{
return Validator::make($data, [
‘name’ => ‘required|string|max:255′,
’email’ => ‘required|string|email|max:255|unique:users’,
‘phone’ => ‘required|numeric|digits_between:10,13’,
‘test_field’ => ‘required|string|max:255’,
‘password’ => ‘required|string|min:6|confirmed’,
]);
}
and
protected function create(array $data)
{

return User::create([
‘name’ => $data[‘name’],
’email’ => $data[’email’],
‘phone’ => $data[‘phone’],
‘test_field’ => $data[‘test_field’],
‘password’ => bcrypt($data[‘password’]),
]);
}

vi. add App/user.php
protected $fillable = [
‘name’, ’email’, ‘phone’, ‘test_field’, ‘password’,
]

Leave a Reply

Your email address will not be published. Required fields are marked *