如何使用laravel在employee表中插入多个角色

时间:2018-06-08 16:00:50

标签: php laravel-5 eloquent

我正在构建API,我正在努力解决多个员工角色的商店数据,如何存储,你能给我一些例子, 抱歉使用外语。 Karyawan =员工, Jabatan =作用

Karyawan.php

public function jabatan() {
    return $this->hasMany('\App\Jabatan','id_jabatan');
}

Jabatan.php

public function karyawan(){
        return $this->belongsTo('\App\Karyawan','id_jabatan');
}

如何在商店功能的KaryawanController应该...

this is how its look like in front end

this is my model and controller

我创建了我的数据透视表,我收到了这个错误

SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'0'(SQL:插入tb_jabatan_karyawanid_jabatanid_karyawan,{{1} },0)值(0,57,1,2)) ini控制器sama modelnya ..

1 个答案:

答案 0 :(得分:1)

我分别使用UserRole代替KaryawanJabatan(对不起)。 UserRole具有Many-to-Many关系,因为一个User可能有多个Roles,反之亦然。

角色表

+----+------+-----------+
| id | role | timeStamps|
+----+------+-----------+

role_user数据透视表

+----+---------+---------+
| id | user_id | role_id |
+----+---------+---------+

角色模型

public function users(){
    return $this->belongsToMany('App\User');
}

用户模型

public function roles(){
    return $this->belongsToMany('App\Role');
}

RoleUser Pivot

namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;

class RoleUser extends Pivot
{
    protected $table='role_user';
    protected $fillable = [
        'user_id','role_id'
    ];

    public static $role_attach_rules = [
        'roles' => 'required|array|min:1|exists:roles,id'
    ];

    public function user(){
        return $this->hasOne('App\User', 'id', 'user_id');
    }

    public function role(){
        return $this->hasOne('App\Role', 'id', 'user_id');
    }
}

为用户分配角色的控制器代码

在这里,您将通过他们的名字了解这些方法。

public function getAssignRole(){
    $users = User::all();
    $roles=Role::all();
    return View::make('assignrole', compact('users', 'roles'));
}

public function postAssignRole(Request $request){
    $attachvalidator = Validator::make($request->all(),RoleUser::$role_attach_rules);
    if ($attachvalidator->fails()){
        return Redirect::back()->withErrors($attachvalidator)->withInput();
    }
    $user=$request->get('user');
    $role=$request->get('roles');
    $attach = User::find($user)->roles()->attach($role);
    return 'Success';
}

public function detachRole(Request $request){
    $user=$request->get('user_update');
    $role=$request->get('roles_update');
    $attach = User::find($user)->roles()->detach($role);
    return 'detach Success';    
}

查看分配角色的文件(assignrole.blade.php)

//This form is for assigning roles
<form method="post" action="{{route('role.assignrole.post')}}">
{{csrf_field()}}
    <select id="user" name="user" class="select_with_style" required>
        @foreach($users as $user)
        <option value="{{$user['id']}}">{{$user['name']}}</option>
        @endforeach
    </select>
    <select id="roles" name="roles[]" class="select_with_style" multiple="" required>
        @foreach($roles as $role)
        <option value="{{$role['id']}}">{{$role['role']}}</option>
        @endforeach
    </select>
    <br>
    <input type="submit" name="submit" class="submit action-button" value="Submit"/>
</form>

//This is for detaching roles from user
<form method="post" action="{{route('role.detach')}}">
{{csrf_field()}}
    <select id="user_update" name="user_update" class="select_with_style"  required>
        @foreach($users as $user)
        <option value="{{$user['id']}}">{{$user['name']}}</option>
        @endforeach
    </select>
    <select id="roles_update" name="roles_update[]" class="select_with_style" multiple="" required>
        @foreach($roles as $role)
        <option value="{{$role['id']}}">{{$role['role']}}</option>
        @endforeach
     </select>
     <br>
     <input type="submit" name="submit" class="submit action-button" value="Update"/>
</form>

<强>路线

Route::get('role/assignrole', array('as' => 'role.assignrole', 'uses' => 'YourController@getAssignRole'));
Route::post('role/assignrole', array('as' => 'role.assignrole.post', 'uses' => 'YourController@postAssignrole'));
Route::post('role/detach', array('as' => 'role.detach', 'uses' => 'YourController@detachRole'));

希望这有帮助。

  

修改

您可以使用Voyager而不是做这么多工作。它很容易理解,结构合理,我想你会喜欢它。