如何使用附加功能将用户ID和角色ID附加到laravel中的数据透视表?

后街伊姆鲁尔

RegisterController.php

    protected function create(array $data)
    {
        $role = Role::where('name', 'customer')->first();

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'remember_token' => Str::random(60),
        ]);

        $user->role()->attach(['user_id' => $user->id, 'role_id' => $role->id]);

        return $user;
    }

在上面的代码segement我想attachuser_idrole_idpivot其命名为表role_user在时间用户注册

User.php

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

在上面的代码段之间的关系是UserRole

我得到的错误。

SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行(外键约束失败deal_oceanrole_user,约束role_user_role_id_foreign外键(role_id)参考rolesid)ON DELETE CASCADE)(SQL:INSERT INTO role_userrole_iduser_id)值(5, 5),(4、5))

供参考:以下是我创建的表。

         Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('role_id')->default(4);
            $table->string('name');
            $table->string('email')->unique();
            $table->string('photo')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->longText('cartitems')->nullable();
            $table->longText('wishlist')->nullable();
            $table->unsignedBigInteger('discount')->default(0);
            $table->rememberToken();
            $table->timestamps();

            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        });

上表是用于users表的。

       Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('display_name');
            $table->timestamps();
        });

上表是用于roles表的。

       Schema::create('role_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->unsignedBigInteger('role_id');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

            $table->timestamps();
        });

上表是用于role_user表的。

OMR

根据文档

使用$ user->时,不应传递user_id进行附加...

只需传递角色ID:

$user->role()->attach( $role->id);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将用户ID附加到帖子(Laravel 5.3)

如何使用JavaScript将用户输入的时间数据(分钟和秒)附加到数组中?

如何在 Laravel 中使用附加或同步方法在数据透视表中插入多个值

如何在数据透视表Laravel 5中为附加字段附加不同的值

将额外的属性附加到Laravel数据透视表

如何使用附加功能或其他功能将字典添加到列表中?

如何使用Microsoft Graph将用户添加到应用程序组和角色

将角色附加到用户-Laravel

将用户角色:“Admin”附加到单个 html 元素

如何在laravel中使用附加字段更新我的数据透视表

如何从laravel中的用户获取数据透视表中的角色名称?

如何附加到div id

如何遍历excel工作表和数据透视表。然后将它们附加到一个daraframe中

需要将用户数据附加到数组

Laravel-使用数据透视表唯一的行ID从数据透视表列中获取值

Laravel将数据数组附加到数据透视

如何使用 swift UI 中的表单功能将用户输入发送到数据库?

如何将用户CSS附加到Microsoft Edge?

如何使用@JoinTable在单个表中存储用户ID,角色ID和特权ID?

我无法访问附加到数据透视表的数组?

SQL - 如何从 Mysql 中的另一个表插入附加到 Id 的新行

如何将用户名附加到文件名中

如何将用户输入的值附加到df的不同列中?

正确使用IO重定向在Linux脚本中将用户输入附加到文件中?

如何在laravel 5中建立用户和角色关系?

使用 jquery 将动态数据附加到表体中

如何将函数直接附加到 jQuery 中的 ID?

无法将用户附加到组

如何使用“接口”将数据附加到表中?在 Angular 6 中使用 Typescript