laravel-5.5使用 permission 出现 Column 'model_id' cannot be null?

是老马呀
  • 582

错误信息

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'model_id' cannot be null (SQL: insert into `ks_model_has_permissions` (`model_id`, `model_type`, `permission_id`) values (, App\User, 2))

调下面这句出现的错误

$user->givePermissionTo('add articles');

详细代码片段

appUser.php

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

appHttpControllersHomeController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use app\User;
class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index(User $user)
    {
             $user->givePermissionTo('add articles');
    }
}
回复
阅读 4.4k
3 个回答
$user = User::findOrFail($id); //$id 指的是users的ID,也就是用户ID

那么model_id指的是什么? 就是App\User User模型的ID

相关资料

资料1
资料2

 Column 'model_id' cannot be null (SQL: insert into `ks_model_has_permissions` (`model_id`, `model_type`, `permission_id`) values (, App\User, 2))
  • 很明显了啊 model_id 字段不能为空,你看看你 SQL : values (, App\User, 2)

values ('', AppUser, 2) 没有用过,但没值也给传个空的吧

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏