[英]User Permission After Registration in Laravel
在我的数据库中,我有这个
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->string('admin')->nullable();
$table->string('usertype')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
这意味着在我的表格中我有这个
<select name="usertype">
<option>Select Usertype</option>
<option value="1">Landlord</option>
<option value="2">Agent</option>
<option value="3">Tenant</option>
</select>
用户可以 select 是他们的用户类型。 我的 RegisterController 中有这个
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'username' => $data['username'],
'usertype' => $data['usertype'],
'password' => Hash::make($data['password']),
]);
}
当作为房东的用户注册时,它会将他们带到http://127.0.0.1:8000/dashboard
我怎样才能使每个用户的内容不同,即 1. 我希望房东能够添加房产 2. 我希望租户能够编辑个人资料,查看他们购买的房产。 3. 我希望代理能够像房东一样添加房产。
我怎样才能做到这一点?
您可以使用Laravel 策略。 例如,如果你有一个 PropertyController,你可以有一个 PropertyPolicy 的方法:
/**
* Determine whether the user can create models.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
return $user->usertype == 'Landlord';
}
在视图中,您可以执行以下操作:
@can('create', $property)
// link to create property
@endcan
在https://laravel.com/docs/7.x/authorization#creating-policies了解有关Laravel 策略的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.