Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDOException::(“SQLSTATE[23000]:完整性约束冲突:1062重复条目';管理员';用于键';角色&"u name"unique&")_Php_Mysql_Sql_Laravel_Entrust - Fatal编程技术网

Php PDOException::(“SQLSTATE[23000]:完整性约束冲突:1062重复条目';管理员';用于键';角色&"u name"unique&")

Php PDOException::(“SQLSTATE[23000]:完整性约束冲突:1062重复条目';管理员';用于键';角色&"u name"unique&"),php,mysql,sql,laravel,entrust,Php,Mysql,Sql,Laravel,Entrust,我是新来的拉拉维尔委托包。我正在尝试运行数据库种子程序迁移,但每次这样做都会导致以下错误 PDOException::(“SQLSTATE[23000]:完整性约束冲突:1062 键“角色\名称\唯一性”的重复条目“管理员”) 谢谢你的帮助 这是DATABASESEEDER.PHP <?php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use App\Permission; use App\Use

我是新来的拉拉维尔委托包。我正在尝试运行数据库种子程序迁移,但每次这样做都会导致以下错误

PDOException::(“SQLSTATE[23000]:完整性约束冲突:1062 键“角色\名称\唯一性”的重复条目“管理员”)

谢谢你的帮助

这是DATABASESEEDER.PHP

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Permission;
use App\User;
use App\Role;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
        public function run()
        {
          DB::table('users')->delete();
          // Create Admin Role
          $role = [
              'name'=> 'admin',
              'display_name'=> 'Admin',
              'description'=>'Full Permission'  
        ];
        $role = Role::create($role);

        /* Sets role permissions for users
          Get all permissions and attach them to the role */
          $permission::Get();
          foreach($permissions as $key => $value){
              $role->attachPermission($value);
          }
          //create Admin user
          $user = [
              'name'=> 'Admin User',
              'email'=>'admin@test.com',
              'Password'=> Hash::make('newasd123')
          ];
          $user = User::create($user);
          // set user role
          $user->attachRole($role);
        }
    }


您是如何迁移的,您使用的是什么命令?看起来您的
角色
表没有被截断,因此当您尝试
$role=role::create($role)时,角色
管理员
已经存在。我正在使用数据库播种器。我终于能够重新考虑这个问题。这是因为我没有发布委托包的供应商文件。运行
php artisan vendor:publish
并重新运行迁移后,问题得到解决。最初,它无法注册供应商包中定义的多对多关系
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class EntrustSetupTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return  void
     */
    public function up()
    {
        DB::beginTransaction();

        // Create table for storing roles
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });

        // Create table for associating roles to users (Many-to-Many)
        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();

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

            $table->primary(['user_id', 'role_id']);
        });

        // Create table for storing permissions
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });

        // Create table for associating permissions to roles (Many-to-Many)
        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();

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

            $table->primary(['permission_id', 'role_id']);


        });

        DB::commit();
    }

    /**
     * Reverse the migrations.
     *
     * @return  void
     */
    public function down()
    {
        Schema::drop('permission_role');
        Schema::drop('permissions');
        Schema::drop('role_user');
        Schema::drop('roles');
    }
}