从数据库中删除数据 JSON 之一
Delete one of data JSON from Database
我想从数据库中删除 JSON 但我不能
我在控制器中的删除功能:
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();
}
更新
这是我的编辑控制器:
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'));
}
这是我的观点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', [$pro->id]) !!}" class='btn btn-default btn-xs' onclick="return confirm('Are you sure?')">Delete</a>
</div>
@endforeach
我点击了删除按钮,但它不起作用。
最新更新
我的删除功能
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();
}
我的观点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
我使用那个代码,但它也不起作用...
看起来像你的函数,findWithoutFail($id)
找不到 $id
。
尝试转储 $product
和 $photo
以查看它是否真的返回 $id
的数据
目前您正在使用 $id
获取 photo_list
列并尝试使用相同的 $id
删除它,这就是它不起作用的原因。假设 $id
是 23,那么 photo_list
的 json 数组中没有 23 id
现在,如果你想删除 en 元素,你需要 id
个图像,使用那个 id
你可以像这样删除:
从所有数组中删除键
foreach($photo as $key => $value) {
if($value['id'] == '1') { // assumed 1 to be removed
unset($photo[$key]);
}
}
如果您想删除整个 JSON,您可以将 photo_list
列的值更新为 NULL
编辑
请检查以下示例:
$photo ='[{"id": "1","name": "test"},{"id": "2","name": "test"}]';
$photo_obj = json_decode($photo,true); // to get array
$result =[];
foreach($photo_obj as $key => $value) {
if($value['id'] != '1') { // assumed id = 1 to be removed
$result[] = $value;
}
}
print_r(json_encode($result));
我想从数据库中删除 JSON 但我不能
我在控制器中的删除功能:
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();
}
更新 这是我的编辑控制器:
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'));
}
这是我的观点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', [$pro->id]) !!}" class='btn btn-default btn-xs' onclick="return confirm('Are you sure?')">Delete</a>
</div>
@endforeach
我点击了删除按钮,但它不起作用。
最新更新
我的删除功能
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();
}
我的观点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
我使用那个代码,但它也不起作用...
看起来像你的函数,findWithoutFail($id)
找不到 $id
。
尝试转储 $product
和 $photo
以查看它是否真的返回 $id
目前您正在使用 $id
获取 photo_list
列并尝试使用相同的 $id
删除它,这就是它不起作用的原因。假设 $id
是 23,那么 photo_list
现在,如果你想删除 en 元素,你需要 id
个图像,使用那个 id
你可以像这样删除:
从所有数组中删除键
foreach($photo as $key => $value) {
if($value['id'] == '1') { // assumed 1 to be removed
unset($photo[$key]);
}
}
如果您想删除整个 JSON,您可以将 photo_list
列的值更新为 NULL
编辑
请检查以下示例:
$photo ='[{"id": "1","name": "test"},{"id": "2","name": "test"}]';
$photo_obj = json_decode($photo,true); // to get array
$result =[];
foreach($photo_obj as $key => $value) {
if($value['id'] != '1') { // assumed id = 1 to be removed
$result[] = $value;
}
}
print_r(json_encode($result));