mercredi 27 mars 2019

mysql error error SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails?

I am associating my user_id to id on the users table. When I send my form I get this error. I have looked around but haven't found a specific answer to my case. I am expecting the user_id to associate with the id on users table.

// My model
class Project extends Model
{

// protec only specified data
protected $fillable = [
'first_name',
'last_name',
'date_wanted',
'concerns',
'phone_me',
'phone_num',
'user_id'

];

// Patient Form send to database
Project::create(request([

'first_name',
'last_name',
'date_wanted',
'concerns',
'phone_me',
'phone_num',
'user_id'
]));


{ 
// Patient Form validate
request()->validate([
'first_name'=> ['required', 'min:3'],
'last_name'=> ['required', 'min:3'],
'date_wanted'=> 'required',
]);


//Project database table

public function up(){
Schema::create('project', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('date_wanted');
            $table->string('phone_me')->nullable();
            $table->string('phone_num')->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

}

// Users database Table

  {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        
        });
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire