我尝试了很多时间,但是失败了。 例如:我想要的结果是,如果我将产品插入数据库并选择多个类别并将它们存储在不同的表中,然后显示如下错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'space.category_product' doesn't exist (SQL: insert into `category_product` (`category_id`, `product_id`) values (1, ))
如果我重命名数据库表从category_products手动到category_product,请在下面显示新错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into `category_product` (`category_id`, `product_id`) values (1, ))
这是我的数据库代码
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('model');
$table->string('slug')->unique();
$table->string('availability');
$table->float('price');
$table->longText('images');
$table->text('short_detail');
$table->text('feature');
$table->longText('purchase_delivery');
$table->longText('replace_policy');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
Schema::create('category_products', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
这是我的模特:
class Category extends Model
{
public function Products()
{
return $this->belongsToMany(Product::class);
}
}
class Product extends Model
{
public function Categories()
{
return $this->belongsToMany(Category::class);
}
}
ProductController:
public function store(Request $request)
{
if ($request->hasFile('file')){
foreach ($request->file as $file) {
$fileExt = $file->getClientOriginalExtension();
$uniqId = uniqid('img-');
$fileName[] =$uniqId.'.'.$fileExt;
$Name = $uniqId.'.'.$fileExt;
$file->move('public/uploads',$Name);
}
$images = implode(",",$fileName);
$product = new Product();
$product->Categories()->attach($request->categories_id);
$product->name= $request->input('name');
$product->model= $request->input('model');
$product->slug= $request->input('slug');
$product->availability= $request->input('availability');
$product->price= $request->input('price');
$product->images= $images;
$product->short_detail= $request->input('short_detail');
$product->feature= $request->input('feature');
$product->purchase_delivery= $request->input('purchase_delivery');
$product->replace_policy= $request->input('replace_policy');
if ($product->save()) {
return redirect()->route('product.index')
->with('success', 'Product Added successfully');
}
return back()->withInput()->with('errors', 'Error Adding New Product');
}
}
答案 0 :(得分:0)
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
您输入的错字应为category_product
。
您的表格名称应按字母顺序排列,并使用单数名称。
如果您不想更改表名
public function Categories()
{
return $this->belongsToMany(Category::class, 'category_products,'category_id', 'product_id');
}
我的ProductController:
public function store(Request $request)
{
if ($request->hasFile('file')){
foreach ($request->file as $file) {
$fileExt = $file->getClientOriginalExtension();
$uniqId = uniqid('img-');
$fileName[] =$uniqId.'.'.$fileExt;
$Name = $uniqId.'.'.$fileExt;
$file->move('public/uploads',$Name);
}
$images = implode(",",$fileName);
$product = new Product();
$product->name= $request->input('name');
$product->model= $request->input('model');
$product->slug= $request->input('slug');
$product->availability= $request->input('availability');
$product->price= $request->input('price');
$product->images= $images;
$product->short_detail= $request->input('short_detail');
$product->feature= $request->input('feature');
$product->purchase_delivery= $request->input('purchase_delivery');
$product->replace_policy= $request->input('replace_policy');
$product->save();
$product->Categories()->attach($request->categories_id);
if ($product->save()) {
return redirect()->route('product.index')
->with('success', 'Product Added successfully');
}
return back()->withInput()->with('errors', 'Error Adding New Product');
}
}
在$product->categories()->attach($category_id)
之后使用$product->save()
答案 1 :(得分:0)
您可以在这里使用模型
class Category extends Model
{
public function Products()
{
return $this->belongsToMany(Product::class, 'category_products');
}
}
class Product extends Model
{
public function Categories()
{
return $this->belongsToMany(Category::class, 'category_products);
}
}
category_products
是数据透视表。
现在保存
$product = Product::create(['name' => 'product1']);
$product->Categories()->sync([1, 3, 4]);
OR
$product->Categories()->attach([1, 3, 4]);
差异
同步:您还可以使用sync方法来构建多对多关联。 sync方法接受一组ID放置在 中间表。不在给定数组中的所有ID都将是 从中间表中删除。因此,此操作之后 完成后,只有给定数组中的ID将存在于 中间表:
附加::与添加相同,它将新值附加到现有值上