User Model
For understanding, how the model code works, please refer to country model or company model
Model class
The roles function return all the permissions (function roles from database) for a authenticated user
public function roles()
{
return $this->hasMany(FunctionRoleUser::class);
}
The below function check if authenticated has a given permission/function code.
public function hasCode($user, $code)
{
if ($user->roles()->where('code', $code)->first()) {
return true;
}
return false;
}
We need to include the following import statement(s)
use Illuminate\Database\Eloquent\SoftDeletes;
Replace the code with the following one. Beside bult-in user model, we add staffId, company_id and joinDate.
Note: Please use the copy button to copy the source code.
class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes;
protected $fillable = [
'name',
'email',
'password',
'staffId',
'company_id',
'joinDate',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles()
{
return $this->hasMany(FunctionRoleUser::class);
}
public function hasCode($user, $code)
{
if ($user->roles()->where('code', $code)->first()) {
return true;
}
return false;
}
}
Migration class
Replace the up() and down() methods with the code below.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('staffId');
$table->date('joinDate');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::dropIfExists('users');
}
To create/update the table to database, we need to run artisan migrate command
php artisan migrate