Use this :
// Detach a single image for a product ... $product->images()->detach($imageId);
// Detach all images for the given product ... $product->images()->detach();
Hi,
I have a pivot table that is storing the image_path and product_id. I can update and store with no issues. I want to know how to unlink and delete these pivot table entries at the same time, so the records get deleted and the image itself.
Controller
public function destroy($id)
{
$product = Product::find($id);
unlink(public_path($product->main_image));
$product->attributesProducts()->detach();
$product->imageProducts()->detach();
$product->delete();
return redirect()->route('products.index')->with('error', 'Product deleted successfully');
}
Product Model
public function imageProducts()
{
return $this->belongsToMany(ImageProduct::class, 'image_products', 'product_id', 'image_path');
}
Hey I found a work around, I sadly don't have much time on this project:
public function update(Request $request, $id)
{
$product = Product::find($id);
if($request->hasfile('main_image')){
/* unlink(public_path($product->main_image)); */
$image = $request->file('main_image');
$filename = 'cyberware-'.$image->getClientOriginalName();
$location = public_path('storage/frontend/products/'.$filename);
Image::make($image)->save($location);
$product->main_image = Storage::url('frontend/products/'.$filename);
}
$product->title = $request->title;
$product->alt = $request->alt;
$product->slug = $request->slug;
$product->excerpt = $request->excerpt;
$product->description = $request->description;
$product->specifications = $request->specifications;
$product->features = $request->features;
$product->price = $request->price;
$product->onsale_price = $request->onsale_price;
$product->stock = $request->stock;
$product->type = $request->type;
$product->featured = $request->featured;
$product->status = $request->status;
$product->save();
//Images
if($request->hasfile('images') && $product->save()) {
foreach(DB::table('image_products')->where('product_id', '=', $product->id)->get() as $imagepath){
unlink(public_path($imagepath->image_path));
DB::table('image_products')->where('product_id', '=', $product->id)->delete();
}
foreach($request->images as $image){
$filename = $image->getClientOriginalName();
$location = public_path('storage/frontend/products/extras/'.$filename);
Image::make($image)->save($location);
if(DB::table('image_products')->where('product_id', '==', null)){
DB::table('image_products')->insert([
'image_path' => Storage::url('frontend/products/extras/'.$filename),
'product_id' => $product->id,
]);
} else {
DB::table('image_products')->where('product_id', '==', $product->id)->update([
'image_path' => Storage::url('frontend/products/extras/'.$filename),
'product_id' => $product->id,
]);
}
}
}
//Tags, Categories & Attributes
if($product->save()){
$product->tags()->sync($request->tags);
$product->categories()->sync($request->category);
$product->attributes()->sync($request->attribute);
}
return redirect()->route('products.index',)->with('warning', 'Product updated successfully');
}
Please or to participate in this conversation.