拉拉维尔 |删除功能 - 如何从日历事件中删除照片



如何在编辑日历的事件视图中从日历的事件中删除照片?在事件列表中,我删除了方法,它有效。现在,当我在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.hp:的片段

<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包围按钮,例如:

<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>

如果用户接受删除照片,那么JS中的confirm函数应该提交表单。还记得在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');
}

最新更新