销毁方法无效
我正在尝试从我的数据库中删除图像,但是我收到以下错误:
未定义变量:投资组合(查看:C:\ MyProjects \ bubblehouseProject \ resources \ views \ admin \ portfolio \ addPortfolio.blade.php)
我认为我在Laravel应用程序中尝试实现的逻辑有问题。我正在使用Eloquent ORM。
控制器:
public function Destroy($id) {
$portfolio = new Portfolio();
$portfolio::find($id);
$portfolio->delete();
return redirect('addPortfolio')->with('delete', 'The image has been successfully deleted!');
}
路线:
Route::get('addPortfolio/{id}', 'AddPortfolioController@Destroy');
查看:
<form action="{{ route('addPortfolioController.Destroy', $portfolio->id) }}">
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
{{ method_field('DELETE') }}
</form>
我不完全确定这是从数据库中删除数据的正确方法。
答案 0 :(得分:3)
<强>路线强>
Route::delete('addPortfolio/{id}', 'AddPortfolioController@Destroy')->name('portfolio.destroy');
查看强>
<form action="{{ route('portfolio.destroy', $portfolio->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
</form>
<强>控制器强>
public function destroy($id)
{
if(Portfolio::destroy($id)) {
return redirect('addPortfolio')->with('success', 'The image has been successfully deleted!');
} else {
return redirect('addPortfolio')->with('error', 'Please try again!');
}
}