Laravel foreach 上的多对多关系错误
Laravel many to many relationshiop error on foreach
我有 2 个 table 称为产品和类别产品 belongsToMany 类别和类别 belongsToMany 产品这里是我的 table 模式
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('image');
$table->string('stock');
$table->string('title');
$table->string('slug')->unique();
$table->string('gender');
$table->text('description');
$table->integer('price');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('restrict')
->onUpdate('restrict');
$table->dateTime('published_at');
});
这是我的类别 table 架构
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('categorydesigner_id')->unsigned()->index();
$table->string('name')->unique();
$table->timestamps();
});
Schema::create('category_product', function (Blueprint $table) {
$table->integer('product_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')
->onDelete('restrict')
->onUpdate('restrict');
$table->foreign('product_id')->references('id')->on('products')
->onDelete('restrict')
->onUpdate('restrict');
$table->timestamps();
});
这是我的控制器
public function productpost(Request $request){
$this->validate($request, [
'title' => 'required|max:255',
'description' => 'required',
'price' => 'required',
'image' => 'image|required',
]);
$designer_name = $request->designer;
$designer_slug = str_random(40);
$designer = designer::where('name', $designer_name)->firstOrCreate(
['name' => $designer_name], ['slug' => $designer_slug]
);
$designer->name = $designer_name;
$designer->slug = $designer_slug;
$designer->save();
$designer_id = $designer->id;
$product = new Product;
$product->title = $request->title;
$product->designer_id = $designer_id;
$product->description = $request->description;
$product->price = $request->price;
$product->stock = $request->stock;
$product->gender = $request->gender;
$product_slug = str_random(40);
$product->slug = $product_slug;
$product->user_id = Auth::user()->id;
$product->published_at = Carbon::now()->format('Y-m-d');
if($request->hasFile('image')) {
$file = Input::file('image');
//getting timestamp
$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
$name = $timestamp. '-' .$file->getClientOriginalName();
$file->move(public_path().'/images/product/', $name);
$product->image = $name;
$thumb = Image::make(public_path().'/images/product/' . $name)->resize(1200,1800)->save(public_path().'/images/product/thumb/' . $name, 90);
}
$product->save();
$productsearch = product::where('slug', $product_slug)->firstorfail();
$product_id = $productsearch->id;
$categoryname = $request->category;
foreach ($categoryname as $name) {
$category = category::where('name', $name)->firstOrCreate(['name' => $name, 'categorydesigner_id' => $designer_id]);
$category->name = $name;
$category->categorydesigner_id = $designer_id;
$category->save();
$category->products()->attach($product_id);
}
return Redirect::back()->with('status', 'Post Success');
}
在我的代码中,如果类别已经拥有而不是获取 ID 并附加,我会创建 firstOrCreate,但我收到错误
SQLSTATE[23000]:违反完整性约束:1062 键 'categories_name_unique' 的重复条目 'swim'
帮我谢谢
既然你无论如何都要保存实例,你不妨使用firstOrNew
:
$category = category::firstOrNew(['name' => $name]);
$category->categorydesigner_id = $designer->id;
$category->save();
$category->products()->attach($product_id);
这应该可以消除错误。
希望对您有所帮助!
我有 2 个 table 称为产品和类别产品 belongsToMany 类别和类别 belongsToMany 产品这里是我的 table 模式
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('image');
$table->string('stock');
$table->string('title');
$table->string('slug')->unique();
$table->string('gender');
$table->text('description');
$table->integer('price');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('restrict')
->onUpdate('restrict');
$table->dateTime('published_at');
});
这是我的类别 table 架构
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('categorydesigner_id')->unsigned()->index();
$table->string('name')->unique();
$table->timestamps();
});
Schema::create('category_product', function (Blueprint $table) {
$table->integer('product_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')
->onDelete('restrict')
->onUpdate('restrict');
$table->foreign('product_id')->references('id')->on('products')
->onDelete('restrict')
->onUpdate('restrict');
$table->timestamps();
});
这是我的控制器
public function productpost(Request $request){
$this->validate($request, [
'title' => 'required|max:255',
'description' => 'required',
'price' => 'required',
'image' => 'image|required',
]);
$designer_name = $request->designer;
$designer_slug = str_random(40);
$designer = designer::where('name', $designer_name)->firstOrCreate(
['name' => $designer_name], ['slug' => $designer_slug]
);
$designer->name = $designer_name;
$designer->slug = $designer_slug;
$designer->save();
$designer_id = $designer->id;
$product = new Product;
$product->title = $request->title;
$product->designer_id = $designer_id;
$product->description = $request->description;
$product->price = $request->price;
$product->stock = $request->stock;
$product->gender = $request->gender;
$product_slug = str_random(40);
$product->slug = $product_slug;
$product->user_id = Auth::user()->id;
$product->published_at = Carbon::now()->format('Y-m-d');
if($request->hasFile('image')) {
$file = Input::file('image');
//getting timestamp
$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
$name = $timestamp. '-' .$file->getClientOriginalName();
$file->move(public_path().'/images/product/', $name);
$product->image = $name;
$thumb = Image::make(public_path().'/images/product/' . $name)->resize(1200,1800)->save(public_path().'/images/product/thumb/' . $name, 90);
}
$product->save();
$productsearch = product::where('slug', $product_slug)->firstorfail();
$product_id = $productsearch->id;
$categoryname = $request->category;
foreach ($categoryname as $name) {
$category = category::where('name', $name)->firstOrCreate(['name' => $name, 'categorydesigner_id' => $designer_id]);
$category->name = $name;
$category->categorydesigner_id = $designer_id;
$category->save();
$category->products()->attach($product_id);
}
return Redirect::back()->with('status', 'Post Success');
}
在我的代码中,如果类别已经拥有而不是获取 ID 并附加,我会创建 firstOrCreate,但我收到错误 SQLSTATE[23000]:违反完整性约束:1062 键 'categories_name_unique' 的重复条目 'swim' 帮我谢谢
既然你无论如何都要保存实例,你不妨使用firstOrNew
:
$category = category::firstOrNew(['name' => $name]);
$category->categorydesigner_id = $designer->id;
$category->save();
$category->products()->attach($product_id);
这应该可以消除错误。
希望对您有所帮助!