gpt4 book ai didi

php - Laravel CRUD 销毁方法不起作用。无法将数据从 View 传递到 Controller 以删除数据

转载 作者:行者123 更新时间:2023-12-02 20:46:49 24 4
gpt4 key购买 nike

销毁方法不起作用

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

Undefined variable: portfolio (View: C:\MyProjects\bubblehouseProject\resources\views\admin\portfolio\addPortfolio.blade.php)

我相信我尝试在 Laravel 应用程序中实现的逻辑有问题。我正在使用 Eloquent ORM。

Controller :

    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>

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

最佳答案

路线

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>

Controller

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

关于php - Laravel CRUD 销毁方法不起作用。无法将数据从 View 传递到 Controller 以删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44078111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com