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 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效