gpt4 book ai didi

laravel - Laravel href与POST

转载 作者:行者123 更新时间:2023-12-04 22:57:47 29 4
gpt4 key购买 nike

我试图通过href Action 将一些数据传递给我的 Controller 。我不知道为什么,但是laravel使用 GET 方法传递数据,但是我需要一个 POST 来代替 GET 。我真的不明白为什么laravel会那样做,却找不到答案。我做了多次,我的语法似乎是正确的。有人可以看看吗?

Blade :

 <td>
@foreach($products as $product)
<a href="{{ action('ProductsController@delete', $product->id ) }}">
<span class="glyphicon glyphicon-trash"></span></a>
{{ $product->name }},
@endforeach
</td>

我的路线:
Route::post('delete', ['as' => 'delete', 'uses' => 'ProductController@delete']);

在我的 Controller 中只是一个:
public function delete()
{
return 'hello'; // just testing if it works
}

错误:
MethodNotAllowedHttpException in RouteCollection.php line 219....

我知道这是一个get方法,因为如果我尝试将数据传递给 Controller ​​,则我的URL如下所示:
blabla.../products/delete?10 

我的语法有什么问题吗?我真的看不出来为什么它使用get方法。
我还尝试了 data-method="post"标记的 <a>现场,但这也不起作用。

感谢您抽出宝贵的时间。

最佳答案

当您使用<a href=example.com>之类的 anchor 链接时,您的方法将始终为GET。这就像在浏览器中打开URL,然后发出GET请求一样。

您应该使用一种形式向 Controller 的delete方法发出该POST请求。假设您具有用于HTML和表单的Illuminate HTML包,则可以执行以下操作:

{!! Form::open(['method' => 'DELETE', 'route' => $route]) !!}
{!! Form::submit('delete', ['onclick' => 'return confirm("Are you sure?");']) !!}
{!! Form::close() !!}

编辑:
带有按钮标签:
{!! Form::open(['method' => 'DELETE', 'route' => $route]) !!}
<button type="submit"><i class="glyphicon glyphicon-remove"></i>Delete</button>
{!! Form::close() !!}

关于laravel - Laravel href与POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37806171/

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