Php Laravel工厂正在使用名称Id而不是给定Id创建PK列

Php Laravel工厂正在使用名称Id而不是给定Id创建PK列,php,laravel,laravel-5,factory,Php,Laravel,Laravel 5,Factory,我有以下工厂: $factory->define(App\Product::class, function (Faker $faker) { return [ 'product_name' => $faker->sentence($nbWords = 4, $variableNbWords = true), 'product_description' => $faker->paragraph($nbSentences = 6,

我有以下工厂:

$factory->define(App\Product::class, function (Faker $faker) {
    return [
        'product_name' => $faker->sentence($nbWords = 4, $variableNbWords = true),
        'product_description' => $faker->paragraph($nbSentences = 6, $variableNbSentences = true),
        'product_price' => $faker->numberBetween($min=500, $max=2000),
    ];
});
以及以下迁移:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('product_id');
            $table->string('product_name', 356)->nullable(false);
            $table->longText('product_description')->nullable();
            $table->integer('product_price')->nullable(false);
            $table->timestamps();
        });
    }
在运行工厂并打印出新创建的产品后,它说PK是“id”而不是“product_id”。当尝试访问$product->product_id时返回null,因为根据工厂,显然不存在该值

在检查DB表时,我可以确认该列的名称为product_id,而不是'id'

我已经跑了:

composer dump-autoload
然后再次运行迁移,但仍然返回错误的数据

谢谢

您可以更改为:

$table->integer('product_id')->primary();

根据文档,您应该通过在模型中设置来覆盖默认值

protected $primaryKey = 'product_id';

不要忘记对表关系(hasManys、belongTos等)进行必要的更改。

我尝试过这样做,但在运行迁移时遇到以下错误:illumb\Database\QueryException:SQLSTATE[42000]:语法错误或访问冲突:1068定义了多个主键(SQL:alter table
products
add primary key
products\u id\u primary
product\u id
)您必须运行php artisan migrate:refresh重置并重新运行所有迁移这正是我想要的,您可以尝试$table->integer(