mysql error error SQLSTATE [23000]: нарушение ограничения целостности: 1452 Не удается добавить или обновить дочернюю строку: ограничение внешнего ключа не выполнено? - PullRequest
0 голосов
/ 27 марта 2019

Я связываю свой user_id с идентификатором в таблице пользователей.Когда я отправляю свою форму, я получаю эту ошибку.Я посмотрел вокруг, но не нашел конкретного ответа на мой случай.Я ожидаю, что user_id ассоциируется с идентификатором в таблице пользователей.Отредактируйте к вопросу, я добавил формы.

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

// формы для отправки

<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>

// Мой класс модели Project расширяет модель {

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

        });
    }

1 Ответ

0 голосов
/ 05 апреля 2019

Я решил проблему путем слияния с использованием array_merge

Project::create(array_merge(request([

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





Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...