使用POST的Laravel href

Wel*_*lNo 4 action parameter-passing laravel

我试图通过一个动作将一些数据传递给我的控制器href.我不知道为什么,但laravel使用GET方法传递数据,但不是GET我需要POST.我真的不明白为什么laravel这样做并且找不到答案.我做了多次,我的语法似乎是正确的.有人可以看看吗?

刀:

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

我的路线:

Route::post('delete', ['as' => 'delete', 'uses' => 'ProductController@delete']);
Run Code Online (Sandbox Code Playgroud)

在我的控制器只是一个:

public function delete()
{
    return 'hello'; // just testing if it works
}
Run Code Online (Sandbox Code Playgroud)

错误:

MethodNotAllowedHttpException in RouteCollection.php line 219....
Run Code Online (Sandbox Code Playgroud)

我知道这是一个get方法,因为如果我试图将数据传递给我的控制器,我的URL看起来像这样:

blabla.../products/delete?10 
Run Code Online (Sandbox Code Playgroud)

我的语法有什么问题吗?我真的不明白为什么它使用get方法.我也尝试了:data-method="post"我的<a>标签,但这也没有用.

谢谢你花时间.

the*_*len 8

当您使用锚点建立链接时,<a href=example.com>您的方法将永远是GET.这就像在浏览器中打开URL一样,您发出GET请求.

您应该使用表单将POST请求发送到控制器的delete方法.假设您有HTML和表单的Illuminate HTML包,您可以这样做:

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

编辑: 使用按钮标记:

{!! Form::open(['method' => 'DELETE', 'route' => $route]) !!}
<button type="submit"><i class="glyphicon glyphicon-remove"></i>Delete</button>
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)