noblemfd
1 month ago
163
5
Laravel

How to Set Role from Employee Controller

Posted 1 month ago by noblemfd

I am using Laravel Spatie for User Role and Permission Management. Initially, I have set the Role from user Contoller and user view blade using

UserController

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');
}

User Model

class User extends Authenticatable
{
  use Notifiable;
  use HasRoles;

protected $fillable = ['name', 'employee_id','email', 'password', 'remember_token'];

public function setPasswordAttribute($input)
{
    if ($input)
        $this->attributes['password'] = app('hash')->needsRehash($input) ? Hash::make($input) : $input;
}


public function role()
{
    return $this->belongsToMany(Role::class, 'role_user');
}   
}

view

    <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>

But now, I have another table called employee. And from there I want set the User Authentication and Role

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');
}
}

Employee View

<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>

I got stuck in the Employee Controller, and View.

How do I now re-write EmployeeController, Model and View to be able to Add Roles from there.

Thank you.

Please sign in or create an account to participate in this conversation.