Здравствуйте. Имеются две модели
class Product extends Model
{
public function categories(){
return $this->belongsToMany('App\Category');
}
}
и
class Category extends Model
{
public function children(){
return $this->hasMany(self::class, 'parent_id');
}
}
А также миграции со сводной таблицей
class CreateProductCategoryTable extends Migration
{
public function up()
{
Schema::create('product_category', function (Blueprint $table) {
$table->BigIncrements('id');
$table->BigInteger('product_id')->unsigned();
$table->BigInteger('category_id')->unsigned();
$table->timestamps();
});
Schema::table('product_category', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products')
->onDelete('cascade')->onUpdate('cascade');
$table->foreign('category_id')->references('id')->on('categories')
->onDelete('cascade')->onUpdate('cascade');
});
}
public function down()
{
Schema::table('product_category', function (Blueprint $table){
$table->dropForeign('product_category_product_id_foreign');
$table->dropForeign('product_category_category_id_foreign');
});
Schema::dropIfExists('product_category');
}
При попытке присвоить категорию к продукту с помощью этого кода,
public function store(Request $request)
{
$product = Product::create($request->except('categories'));
if($request->has('categories')){
$product->categories()
->attach($request->input('categories'));
}
return redirect()->route('product.index');
}
возникает ошибка
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.category_product' doesn't exist (SQL: select `categories`.*, `category_product`.`product_id` as `pivot_product_id`, `category_product`.`category_id` as `pivot_category_id` from `categories` inner join `category_product` on `categories`.`id` = `category_product`.`category_id` where `category_product`.`product_id` = 5) (View: D:\OSPanel\domains\test.local\resources\views\product\_categories.blade.php)