به خطای SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'products_slug_unique' (SQL: insert into products
(title
, sku
, slug
, status
, price
, discount_price
, description
, brand_id
, meta_desc
, meta_title
, meta_keywords
, user_id
, updated_at
, created_at
) values (آبمیوه گاز دار پرتقال موز, 61892, , 1, 6500, , <p>توضیحات</p>, 3, پرتقال موز شیشه هوفنبرگ, پرتقال موز شیشه هوفنبرگ, پرتقال موز شیشه هوفنبرگ, 1, 2021-01-19 13:51:32, 2021-01-19 13:51:32))
برخوردم
دوستان لطفا راهنمایی کنید
@hesammousavi
@ali.bayat
@abedim910
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('sku')->unique();
$table->string('slug')->unique();
$table->tinyInteger('status');
$table->float('price');
$table->float('discount_price')->nullable();
$table->text('description');
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->unsignedInteger('brand_id');
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
class Product extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class);
}
public function brand()
{
return $this->belongsTo(Brand::class);
}
public function user()
{
return $this->belongsTo(Brand::class);
}
public function attributeValues()
{
return $this->belongsToMany(AttributeValue::class, 'attributevalue_product', 'product_id', 'attributeValue_id');
}
public function photos()
{
return $this->belongsToMany(Photo::class);
}
public function orders()
{
return $this->belongsToMany(Order::class);
}
}
public function checkSKU($number)
{
return Product::where('sku', $number)->exists();
}
function makeSlug($string)
{
$string = strtolower($string);
$string = str_replace(['؟', '?'], '', $string);
return preg_replace('/\s+/u', '-', trim($string));
}
public function store(Request $request)
{
$newProduct = new Product();
$newProduct->title = $request->title;
$newProduct->sku = $this->generateSKU();
$newProduct->slug = $this->makeSlug($request->slug);
$newProduct->status = $request->status;
$newProduct->price = $request->price;
$newProduct->discount_price = $request->discount_price;
$newProduct->description = $request->description;
$newProduct->brand_id = $request->brand;
$newProduct->meta_desc = $request->meta_desc;
$newProduct->meta_title = $request->meta_title;
$newProduct->meta_keywords = $request->meta_keywords;
$newProduct->user_id = 1;
$newProduct->save();
$attributes = explode(',', $request->input('attributes')[0]);
$photos = explode(',', $request->input('photo_id')[0]);
$newProduct->categories()->sync($request->categories);
$newProduct->attributeValues()->sync($attributes);
$newProduct->photos()->sync($photos);
Session::flash('success', 'محصول با موفقیت اضافه شد.');
return redirect('/administrator/products');
}
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟