I want to make the user an administrator who at the entrance to the site conditions are checked == user login , if so then give the right to remove or edit product. This is my code so far:
@if(Auth::check())
<p><a href="#" class="btn btn-success" role="button">Edite</a> <a href="#" class="btn btn-danger" role="button">Remove</a></p>
@endif
How equate Auth::check()
with my login?
First of all you need to create the role table that contain the roles details. (I'm assuming Each user may have multiple roles not only Administrator)
Roles Table:
Schema::create('role', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Then
user_role Table:
Schema::create('user_role', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('user_id')
->references('id')->on('users');
$table->foreign('role_id')
->references('id')->on('roles');
});
Create Role model:
php artisan make:model Role
Afterwards add the roles relationship to the user model:
class User extends Authenticatable {
public function roles() {
return $this->belongsToMany(Role::class, 'user_role');
}
}
To check if user has Administrator role you can do something like:
@if($user->roles()->where('name', 'Administrator')->exists())
enter code here
@endif
Or instead of doing this statement you can put as function in the User model as below:
public function isAdministrator() {
return $this->roles()->where('name', 'Administrator')->exists();
}
Then in your model you can call it:
@if(Auth::user()->isAdministrator())
enter code here
@endif
Other possibility (1 - 1) Relation
First add is_admin column in your migration:
public function up()
{
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('is_admin')->default(0);
$table->rememberToken();
$table->timestamps();
});
Then you can check:
@if($user->is_admin)
@endif
Standard way is to do what xdevnull mentioned. But for small projects you can do something like this:
If(Auth::check() && Auth::user()->isAdmin()) {
dd("you are admin") ;
}
And in your User model create a method isAdmin()
function isAdmin() {
$admin_emails = config('settings.admin_emails');
if(in_array($this->email, $admin_emails) return true;
else return false;
}
Of course you need to create a config file called settings in your app/config folder.
And put this in settings.php
<?php
return ['admin_emails' => explode(', ', env('ADMIN_EMAILS'));]
And finally in your .env file add this: [email protected],[email protected]
Add as much emails as you want spayed by commas.
Now if logged in user has email as [email protected] or [email protected] then she is Admin.
Also notice I have added admin emails in env file so that admins can be changed depending on the environment. In development usually developer is the admin while in production somebody else (client?) is the admin.
P.S. don't forget to run php artisan config:cache
after creating your settings.php file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With