【发布时间】: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>
<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