douzao5487 2017-07-11 04:49
浏览 93
已采纳

从数据库中删除一个数据JSON

I want to delete JSON from database but I can't

enter image description here

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>
  • 写回答

2条回答 默认 最新

  • duanluanlang8501 2017-07-11 05:15
    关注

    Currently you are getting photo_list column using $id and trying to remove it using the same $id that's why it is not working. Suppose $id is 23, then you don't have 23 id in your json array of photo_list

    Now if you want to remove en element you need to have id of single a image, using that id you can remove like:

    To remove key from all array

    foreach($photo as $key => $value) {
       if($value['id'] == '1') { // assumed 1 to be removed
        unset($photo[$key]);
       }
    }
    

    if you want to remove whole JSON, you can update that photo_list column's value as NULL

    EDIT

    please check the below example:

    $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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题