从数据库

时间:2017-07-11 04:49:39

标签: php json database laravel-5.3

我想从数据库中删除JSON,但我不能

enter image description here

我在控制器中的删除功能:

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
&#13;
&#13;
&#13;

我删除了我的按钮删除但它不起作用。

最新更新

我的删除功能

&#13;
&#13;
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();
    }
&#13;
&#13;
&#13;

我的观点blade.php

&#13;
&#13;
@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
&#13;
&#13;
&#13;

我使用该代码,但它也不起作用......

2 个答案:

答案 0 :(得分:0)

似乎是您的功能,findWithoutFail($id)无法找到$id

尝试转储$product$photo,看看它是否真的返回了$id

的数据

答案 1 :(得分:0)

目前,您正在使用photo_list获取$id列,并尝试使用相同的$id删除它,这就是它无效的原因。假设$id为23,那么你的json数组photo_list中没有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));