Laravel href with POST

28,485

Solution 1

When you make a link with an anchor like <a href=example.com> your method will always be GET. This is like opening a URL in your browser, you make a GET request.

You should use a form to make that POST request to the delete method of the controller. Assuming you have the Illuminate HTML package for HTML and forms, you could do this:

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

EDIT: With a button tag:

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

Solution 2

Best for laravel 7. First define the form with given form id.

@auth
   <form id="logout-form" action="{{ route('logout') }}" method="POST" 
     style="display: none;">
       @csrf
  </form>
 @endauth

Then you can use the anchor tag for logout operation.In background,javascript working.Wherever the logout was fire then action to given form method of post method and logout route was working.

<a class="nav-link dropdown-toggle text-muted waves-effect waves-dark"
                              href="{{ route('logout') }}" onclick="event.preventDefault();document.getElementById('logout-form').submit();"
id="2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
                                  <i class="mdi mdi-logout"></i>
</a>

Most benefit is the form was not loaded unnecesory.If the user was logged in so far its load otherwise not

Solution 3

Here's your problem:

<a href="{{ action('ProductsController@delete', $product->id ) }}">

Anchor tags are always submitted over GET. It's a HTTP built in and is not Laravel specific.

POST is used when a form is submitted that specifies the POST HTTP Verb or the HTTP method is invoked by an AJAX request that specifies POST as the HTTP Verb.

Instead consider a submit type button in a form that submits what you need.

    <td>
        @foreach($products as $product)
            <form method="POST" action="{{ route('delete') }}">
                <input type="hidden" name="product_id" value="{{ $product->id }}">
                {!! csrf_field() !!}
                <button type="submit" class="btn">
                    <span class="glyphicon glyphicon-trash"></span>
                </button>
            </form>
            {{ $product->name }},
        @endforeach
    </td>

And then in your controller:

    public function delete()
    {
        // 'Die Dump' all of the input from the form / request
        dd( request()->input()->all() );

        // 'Die Dump' the specific input from the form
        dd( request()->input('product_id') );
    }

You will begin to see how GET and POST requests differ in sending of key/value pairs.

For more information:

http://www.tutorialspoint.com/http/http_methods.htm

Solution 4

Laravel 7

By jQuery:

<form id="form" action="{{route('route_name')}}" method="POST">@csrf</form>
<a href="javascript:void(0)" onclick="$('#form').submit()"></a>

By JS:

<form id="form" action="{{route('route_name')}}" method="POST">@csrf</form>
<a href="javascript:void(0)" onclick="document.getElementById('form').submit()"></a>
Share:
28,485
WellNo
Author by

WellNo

Updated on November 08, 2020

Comments

  • WellNo
    WellNo over 3 years

    I'm trying to pass some data to my controller with an action href. I don't know why, but laravel passes the data with GET method, but instead of GET I need a POST. I don't really understand why laravel does that and coulnd't find an answer. I did that multiple times and my syntax seems to be correct. Can somebody have a look over it?

    Blade:

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

    My Route:

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

    In my Controller is just a:

    public function delete()
    {
        return 'hello'; // just testing if it works
    }
    

    Error:

    MethodNotAllowedHttpException in RouteCollection.php line 219....
    

    I know it's a get method, cause if I'm trying to pass the data to my controller, my URL looks like this:

    blabla.../products/delete?10 
    

    Is anything wrong with my syntax? I can't really see why it uses the get method. I also tried a: data-method="post" insite of my <a> tag but this haven't worked either.

    Thanks for taking time.