V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xoxo419
V2EX  ›  问与答

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

  •  
  •   xoxo419 · 2017-12-11 14:52:33 +08:00 · 1408 次点击
    这是一个创建于 2389 天前的主题,其中的信息可能已经有所发展或是发生改变。

    错误信息

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

    详细代码片段

    app\User.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',
        ];
    }
    

    app\Http\Controllers\HomeController.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');
        }
    }
    
    1 条回复    2017-12-13 09:05:11 +08:00
    xoxo419
        1
    xoxo419  
    OP
       2017-12-13 09:05:11 +08:00
    已解决: 详见-> [传送门]( https://segmentfault.com/q/1010000012374893)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5221 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:00 · PVG 16:00 · LAX 01:00 · JFK 04:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.