mercredi 13 novembre 2019

(SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_by' cannot be null)

Hello guys i have a problem while i try to seed my table when i run php artisan db:seed --class=TypeTableSeeder i got this error

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_by' cannot be null "

and this is my code in the file TypeFactory.php i have this code

<?php
use App\Models\Event\EventType;
use App\Models\Auth\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(EventType::class, function (Faker $faker){
    $users = User::all("id")->pluck("id")->toArray();

    return [
        "type"=>$faker->name,
        "prefix"=>Str::random(5),
        "created_by"=>$faker->randomElement($users),
    ];
});

and there is the migration

public function up()
    {
        Schema::create('event_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('type')->unique();
            $table->string('prefix',5)->unique();
            $table->integer('date')->nullable();
            $table->unsignedInteger('created_by');
            $table->foreign('created_by')->references('id')->on('users')
            ->onDelete('restrict')
            ->onUpdate('restrict');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('event_types');
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire