mercredi 12 septembre 2018

Laravel factory is creating PK column with the name Id instead of the given one

I've got the following factory:

$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),
    ];
});

and the following migration:

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();
        });
    }

After running the factory and printing out a newly created product, it says that the PK is 'id' instead of 'product_id'. When trying to access $product->product_id is returning null because obviously that doesn't exists according to the factory.

While inspecting the DB table I can confirm that the column is named product_id and not 'id'.

I already ran:

composer dump-autoload

and then ran the migration again but still getting the wrong data back.

Thanks



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2x43Dwc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire