【问题标题】:Delete the row after removing file from storage从存储中删除文件后删除该行
【发布时间】:2021-02-25 22:13:01
【问题描述】:

一切正常,但它抛出一个错误“试图得到 非对象的属性‘路径’”在这一行 “unlink(“uploads/”.$img->path);” 以及当我刷新行和文件时 已删除。如果有其他方法请告诉我

刀片:

<table class="table table-hover">

  <thead>

    <tr>

     <th scope="col">image</th>
     <th scope="col">title</th>
     <th scope="col">Actions </th>
     </tr>

     </thead>



        <tbody>

  @foreach($data as $img)

<tr>

<th scope="row"> <img src="uploads/{{ $img->path }}" width="50%" /></th>

  <td>{{$img->title}}</td>
   <td>

  <a href='#'><i class="fa fa-edit"    id="updateIcon"   wire:click="selectItem({{ $img->id }}, 'update')" ></i></a>

  &nbsp;&nbsp;

  <a href="#"><i class="fa fa-trash" style='color:red;'    wire:click="selectItem({{ $img->id }}, 'delete')"  data-target="#modalFormDelete"></i></a>

  </td>

    @endforeach

</tr>



       
班级:
   public function selectItem($itemId, $action)
    {
        $this->selectedItem = $itemId;
        if ($action == 'delete') {
             $this->dispatchBrowserEvent('openDeleteModal');
        }
        
        
    }
    public function delete()
    {
        $img=Caroussel_Img::find($this->selectedItem);
        unlink("uploads/".$img->path);//bug here
        Caroussel_Img::where("id",$img->id)->delete();
        $this->dispatchBrowserEvent('closeDeleteModal');
     }

【问题讨论】:

  • 所选项目不存在,根据您的错误消息,您不处理不存在的情况。您还应该考虑添加授权以确保允许用户删除该图像。
  • 您还应该键入循环中生成的任何元素,以确保 Livewire 始终知道什么是什么

标签: php laravel-8 laravel-livewire


【解决方案1】:

这不是一个好主意, 我不知道“Caroussel_Img”里面是什么

但是如果它正在删除文件,在行首添加“@”就不会再打扰你了

@unlink("uploads/".$img-&gt;path);

【讨论】:

  • 抑制错误而不是处理它们是一个非常糟糕的主意。
  • 我说这是个坏主意,不知道那个类里面有什么!
猜你喜欢
  • 1970-01-01
  • 2021-07-07
  • 2017-02-01
  • 1970-01-01
  • 2018-11-19
  • 2020-06-17
  • 2018-07-09
  • 1970-01-01
  • 2017-03-24
相关资源
最近更新 更多