Laravel CRUD破坏方法不起作用。无法将数据从视图传递到控制器以删除数据

时间:2017-05-19 19:56:19

标签: php laravel laravel-5 eloquent crud

销毁方法无效

我正在尝试从我的数据库中删除图像,但是我收到以下错误:

  

未定义变量:投资组合(查看: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>

我不完全确定这是从数据库中删除数据的正确方法。

1 个答案:

答案 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!');
  }
}