[英]How to delete photo from project's public folder before updating data in lumen/laravel?
[英]Laravel | Delete function - how to delete photo from calendar's event
如何在編輯日歷的事件視圖中從日歷的事件中刪除照片? 在事件列表中,我確實刪除了方法,並且該方法有效。 現在,當我嘗試在edit.blade.php中執行相同的操作時,它給出了錯誤:
Call to a member function photos() on null
我有兩個表,一個日歷與許多照片有關系,文件上傳有效,但是我停留在編輯部分。
看一下我的控制器功能:
public function deletePhoto(CalendarRepository $calRepo, $id)
{
$calendars = $calRepo->find($id);
$calendars->photos($id)->delete();
return redirect()->action('CalendarController@edit');
}
這是edit.blade.php的片段:
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
@foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}" style="width:100px; height:auto;"/>
</div>
<a href="{{ URL::to('calendar/deletePhoto/' . $photo->id ) }}" onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</div>
@endforeach
</div>
</div>
我需要從“照片”表中刪除照片,然后重定向到edit.blade.php(關於日歷的特定事件ID)
謝謝你的幫助。
編輯:
<div class="card-body">
<form action="{{ action ('CalendarController@editStore')}}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $calendar->id }}"/>
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
@foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}"/>
</div>
<form method="POST" action="{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}">
@csrf
@method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
</div>
@endforeach
</div>
</div>
<div class="form-group">
<label for="header">Header</label>
<input type="text" class="form-control" name="header" value="{{ $calendar->header }}"/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{ $calendar->description }}"/>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" name="date" value="{{ $calendar->date }}"/>
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
</form>
</div>
您使用相同的$id
查找照片和日歷實例。 不建議您使用GET請求刪除資源,因此更好的方法是在您的路由中添加以下內容:
Route::delete('photo/{photo}', 'PhotosController@delete')->name('photo.delete');
然后,在您的視圖中,您應該在按鈕周圍加上一個窗體,例如:
<form method="POST" action="{{ route('photo.delete', $photo) }}">
@csrf
@method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
然后您confirm
,如果用戶接受刪除的照片在JS函數應該提交表單。 還要記住,在Confirm函數中默認將false返回為默認值,因此默認情況下它不會提交表單。
您的控制器將是:
public function delete(Photo $photo)
{
$photo->delete();
return redirect()->back();
}
-編輯
Route::delete('calendar/{calendar}/photo/{photo}', 'CalendarController@deletePhoto')->name('photo.delete');
表格中的動作可以是:
{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}
控制器中的方法:
public function deletePhoto(Calendar $calendar, Photo $photo)
{
$calendar->photos()->where('id', $photo->id)->delete();
return redirect()->action('CalendarController@edit');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.