Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel href with POST

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.

like image 861
WellNo Avatar asked Jun 14 '16 07:06

WellNo


4 Answers

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() !!}
like image 52
thefallen Avatar answered Nov 01 '22 03:11

thefallen


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

like image 25
Kaushik shrimali Avatar answered Nov 01 '22 02:11

Kaushik shrimali


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

like image 2
Justin Origin Broadband Avatar answered Nov 01 '22 04:11

Justin Origin Broadband


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>
like image 1
Arash Younesi Avatar answered Nov 01 '22 04:11

Arash Younesi