简体   繁体   中英

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. Edit to the question, I added the forms.

 $attributes = $project; $getAll = $attributes; $getAll['user_id'] = auth()->id(); // Patient Form send to database $attributes = Project::create(request([ 'first_name', 'last_name', 'date_wanted', 'phone_me', 'phone_num', 'user_id' ])); return redirect ('/smiledesign/success'); } 

//forms to be sent

<form method="POST" action="/store" id="form">

  <label for=" "> First Name :  <input type="text" name="first_name" required>
  </label>

<br>
<br>
  <label for=" "> Last Name :  <input type="text" name="last_name" required>
        </label>
  <br>
  <br>
  <br>
  <label for=" ">Date Wanted :  <input type="date" name="date_wanted" id="datepicker" required   >
  </label>
  <br>
  <br>

  <label for=" yes"> Phone Me concerning this case:<input type="checkbox" name="phone_me" id="yes"  /></label>
  <label for="yes"> Yes</label>
  <input type="text" name="phone_num" id="phone-num" style="display:none;" />

  <br>
  <br>
  <input type="submit" value="Publish" id="submit">

</form>

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

        });
    }

I solved the problem by merging using the array_merge

Project::create(array_merge(request([

'first_name',
'last_name',
'date_wanted',
'concerns',
'phone_me',
'phone_num',
'user_id'
]),
['user_id' => auth()->id(),
]));```





The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM