我正在使用 Laravel-5.8 开发一个 Web 应用程序。我正在使用 Laravel Spatie 进行用户角色和权限管理。最初,我从用户 Contoller 和用户视图刀片中设置了角色,如下所示:
public function create()
{
$roles = Role::get()->pluck('name', 'name');
return view('admin.users.create', compact('roles'));
}
public function store(StoreUsersRequest $request)
{
$user = User::create($request->all());
$roles = $request->input('roles') ? $request->input('roles') : [];
$user->assignRole($roles);
return redirect()->route('admin.users.index');
}
模型
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
protected $fillable = ['name', 'employee_id','email', 'password', 'remember_token'];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
}
<form action="{{ route("admin.users.store") }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="name">{{ trans('cruds.user.fields.name') }}*</label>
<input type="text" id="name" name="name" class="form-control" value="{{ old('name', isset($user) ? $user->name : '') }}" required>
@if($errors->has('name'))
<em class="invalid-feedback">
{{ $errors->first('name') }}
</em>
@endif
<p class="helper-block">
{{ trans('cruds.user.fields.name_helper') }}
</p>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
<label for="email">{{ trans('cruds.user.fields.email') }}*</label>
<input type="email" id="email" name="email" class="form-control" value="{{ old('email', isset($user) ? $user->email : '') }}" required>
@if($errors->has('email'))
<em class="invalid-feedback">
{{ $errors->first('email') }}
</em>
@endif
<p class="helper-block">
{{ trans('cruds.user.fields.email_helper') }}
</p>
</div>
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
<label for="password">{{ trans('cruds.user.fields.password') }}</label>
<input type="password" id="password" name="password" class="form-control" required>
@if($errors->has('password'))
<em class="invalid-feedback">
{{ $errors->first('password') }}
</em>
@endif
<p class="helper-block">
{{ trans('cruds.user.fields.password_helper') }}
</p>
</div>
<div class="form-group {{ $errors->has('roles') ? 'has-error' : '' }}">
<label for="roles">{{ trans('cruds.user.fields.roles') }}*
<span class="btn btn-info btn-xs select-all">{{ trans('global.select_all') }}</span>
<span class="btn btn-info btn-xs deselect-all">{{ trans('global.deselect_all') }}</span></label>
<select name="roles[]" id="roles" class="form-control select2" multiple="multiple" required>
@foreach($roles as $id => $roles)
<option value="{{ $id }}" {{ (in_array($id, old('roles', [])) || isset($user) && $user->roles->contains($id)) ? 'selected' : '' }}>{{ $roles }}</option>
@endforeach
</select>
@if($errors->has('roles'))
<em class="invalid-feedback">
{{ $errors->first('roles') }}
</em>
@endif
<p class="helper-block">
{{ trans('cruds.user.fields.roles_helper') }}
</p>
</div>
<div>
<input class="btn btn-danger" type="submit" value="{{ trans('global.save') }}">
</div>
</form>
但是现在,我有另一张表叫做雇员。从那里我想设置用户身份验证和角色:
class EmployeeController extends Controller
{
public function create()
{
return view('admin.employees.create');
}
public function store(StoreEmployee $request)
{
$employee = Employee::create([
'fname' => $request->fname,
'lname' => $request->lname,
'birth_date' => $request->birth_date,
'hired_date' => $request->hired_date,
'street' => $request->street,
'town' => $request->town,
'city' => $request->city,
'country' => $request->country,
'phone' => $request->phone,
'gender' => $request->gender,
'email' => $request->email
]);
$otp = str_random(10);
$user = new User;
$user->email = $employee->email;
$user->password = bcrypt($otp);
$user->employee_id = $employee->id;
$user->save();
$employee->update(['user_id' => $user->id]);
Session::flash('success', 'New User created');
return redirect()->route('employees.index');
}
}
模型
class Employee extends Model
{
use SoftDeletes;
protected $fillable = [
'email','user_id','fname', 'lname',
'birth_date','gender','hired_date','phone',
'street','town','city','country'
];
public function user()
{
return $this->hasOne('App\User');
}
}
<form action="{{ route('employees.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group col-md-6">
<label for="fname">First Name: </label>
<input type="text" name="fname" class="form-control">
</div>
<div class="form-group col-md-6">
<label for="lname">Last Name: </label>
<input type="text" name="lname" class="form-control">
</div>
<div class="form-group col-md-12">
<label for="email">Email: </label>
<input type="email" name="email" class="form-control">
</div>
<div class="form-group col-md-4">
<label for="birth_date">Date of Birth: </label>
<input type="date" name="birth_date" class="form-control">
</div>
<div class="form-group col-md-4">
<label for="hired_date">Hired Date: </label>
<input type="date" name="hired_date" class="form-control">
</div>
<div class="form-group col-md-4">
<label for="phone">Contact Phone: </label>
<input type="text" name="phone" class="form-control">
</div>
<div class="form-group col-md-12">
<label for="street">Street: </label>
<input type="text" name="street" class="form-control">
</div>
<div class="form-group col-md-2">
<label for="country">Country: </label>
<input type="text" name="country" class="form-control">
</div>
<div class="text-center">
<button class="btn btn-success" type="submit" >Create</button>
</div>
</form>
我被困在员工控制器和视图中。
我现在如何重新编写 EmployeeController、Model 和 View 以便能够从那里添加角色?谢谢你。