I want to delete JSON from database but I can't
My delete function in controller:
public function deletephoto($id)
{
$product = $this->productRepository->findWithoutFail($id);
$photo = json_decode($product->photo_list,true);
$photos = $photo[$id-1];
unset($photos);
$product->save();
Flash::success('Photo deleted successfully.');
return back();
}
UPDATE Here my edit controller:
public function edit($id)
{
$product = $this->productRepository->findWithoutFail($id);
$store = Store::pluck('name', 'id')->all();
$photo = json_decode($product->photo_list);
//dd($photo);
$category = Category::pluck('name','id')->all();
if (empty($product)) {
Flash::error('Product not found');
return redirect(route('products.index'));
}
return view('products.edit',compact('product','store','category','photo'));
}
Here my view blade.php. I'm using button to delete it.
@foreach($photo as $pro)
<div style="margin-right:10px" class="form-group col-sm-1">
<p><img src="{{ env('FRONTEND_URL') . "/img/products/$product->id/$pro->name"}}" width="100" height="100"/></p>
<a href="{!! route('products.deletephoto', [$pro->id]) !!}" class='btn btn-default btn-xs' onclick="return confirm('Are you sure?')">Delete</a>
</div>
@endforeach
I clik my button delete but it doesn't work.
LASTEST UPDATE
My Delete Function
public function deletephoto($productid,$photoid)
{
$product = $this->productRepository->findWithoutFail($productid);
$photo = json_decode($product->photo_list,true);
foreach($photo as $key => $value) {
if($value['id'] == $photoid) {
unset($photo[$key]);
}
}
return back();
}
my view blade.php
@foreach($photo as $pro)
<div style="margin-right:10px" class="form-group col-sm-1">
<p><img src="{{ env('FRONTEND_URL') . "/img/products/$product->id/$pro->name"}}" width="100" height="100"/></p>
<a href="{!! route('products.deletephoto', [$product->id,$pro->id]) !!}" class='btn btn-default btn-xs' onclick="return confirm('Are you sure?')">Delete</a>
</div>
@endforeach
I use that code but it doesnt work too...
</div>