Answer the question
In order to leave comments, you need to log in
many to many. Why does an error occur when creating a record: id does not exist?
No matter how much I searched for the answer in the documentation, I did not find it.
Here is the structure of the User categories table:
Schema::create('user_categories', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title_ru', 50);
$table->string('title_en', 50);
$table->string('slug', 50);
$table->timestamps();
});
$user_category = new UserCategory();
$user_category->user_id = Auth::user()->id();
$user_category->category_id = $request->category_id;
if ($user_category->save()) {
return response()->json(['response' => 'success']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question