the PUT method does not send messages - laravel

if necessary, I will also put the controller in check although it seems to
me that something is wrong with the method because on the output it shows me a "no message" error and nothing more
is on a piece of my view and the PUT method
{!! Form::model($cattle_inventory, array('route'=>
['cattle_inventories.update',$cattle_inventory->id,'method'=>'PUT']))!!}
<div class="form-group">
{!! Form::label('cow_name','Podaj NazwÄ™ krowy') !!}
{!! Form::text('cow_name',null, ['class'=>'form-control']) !!}
</div>
Route
Route::resource('cattle_inventories','Cattle_inventoryController')->middleware('verified');

The documentation states:
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
Hence you need to adjust your form as such. #method('PUT') simply generates the following HTML:
<input type="hidden" name="_method" value="PUT">

You can try this with Laravel collective
{!! Form::open(['route'=>['your.route', $id]]) !!}
// laravel <=5.5
{!! Form::hidden('_method', 'PUT') !!} //or {{ method_field('PUT') }}
//laravel >=5.6
#method('PUT')
{!! Form::close() !!}
Laravel collective Form model binding
{{ Form::model($cattle_inventory, ['route' => ['cattle_inventories.update', $cattle_inventory->id]]) }}

Related

Why submitting form with PUT method I got GET request?

In my laravel 5.7 app I make form for updating of data, like:
<section class="card-body">
<h4 class="card-title">Edit vote</h4>
<form method="PUT" action="{{ url('/admin/votes/update/'.$vote->id) }}" accept-charset="UTF-8" id="form_vote_edit" class="form-horizontal"
enctype="multipart/form-data">
{!! csrf_field() !!}
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
with routes dined in routes/web.php:
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
...
Route::put('/votes/update/{vote_id}', 'Admin\VotesController#update');
but submitting the form I got request with error:
Request URL: http://local-votes.com/admin/votes/update/22?_token=0CEQg05W4jLWtpF3xB6BGSdz1icwysiDOStLVgHv&id=22&name=gg...
Request Method: GET
Status Code: 405 Method Not Allowed
Why GET request, what is wrong in my form ?
Thanks!
HTML Forms only support GET and POST.
From the docs:
Since HTML forms can't make PUT, PATCH, or DELETE requests, you will
need to add a hidden _method field to spoof these HTTP verbs.
You can use the method_field helper or the #method blade directive to add the hidden input.
<form action="/foo/bar" method="POST">
#method('PUT')
...
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('PUT') }}
...
</form>

laravelcollective/html not finding any of my controllers

I am getting this error from a login page
Action App\Http\Controllers\Admin\LoginController#authenticate not defined. (View: /Applications/XAMPP/xamppfiles/htdocs/lectureme/resources/views/admin/login.blade.php)
I know for certain that this controller exists though, so why is it unable to find it? I have also created a new controller in the controller root and named it TestController, and tried routing to that instead, but that was also apparently not found.
Any suggestions for how to get access to the controller? Form code:
{!! Form::open(['action' => 'LoginController#authenticate']) !!}
<div class="form-group">
<div class="form-group">
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email Address:') !!}
{!! Form::text('email', null, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit('Login', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I have also tried composer dump-autoload and php artisan cache:clear
Make sure you namespaced your controller properly. Assuming you have placed your controller in the App\Http\Controllers\Admin directory it would be:
namespace App\Http\Controllers\Admin

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.
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() !!}
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
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
Laravel 7
By jQuery:
<form id="form" action="{{route('route_name')}}" method="POST">#csrf</form>
By JS:
<form id="form" action="{{route('route_name')}}" method="POST">#csrf</form>

Laravel 5, Forms, TokenMismatchException in VerifyCsrfToken.php line 46

I keep getting the "TokenMismatchException in VerifyCsrfToken.php line 46" error when submitting forms.
This is one of the forms:
{!! Form::model($product, array('url' => 'product/'.$product->id, 'class' => 'form', 'method' => 'PATCH')) !!}
<div class="form-group">
{!! Form::textarea('note', $product->note,
array('class'=>'form-control', 'id'=>'product-note', 'placeholder'=>Lang::get('customtranslation.form_placeholder_note'), 'rows'=>3)) !!}
<br />
<span class="btn btn-link" id="remove-note" role="button"><i class="fa fa-times"></i> {{ Lang::get('customtranslation.button_txt_reset_note') }}</span>
</div>
<div class="form-group">
{!! Form::submit(Lang::get('customtranslation.button_txt_finish_edit_product'), array('class'=>'btn btn-success')) !!}
</div>
<div class="form-group">
<!-- Custom tags -->
{!! Form::label('additional-tags', Lang::get('customtranslation.form_edit_label_additional_tags')) !!}
{!! Form::text('additional-tags','', array('id'=>'additional-tags', 'data-role'=>'tagsinput')) !!}
</div>
{!! Form::close() !!}
The input element with name "_token" gets generated and set as expected.
The strange thing is that this occurs only in Internet Explorer (IE11). Chrome and FF make a submit without any problems.
Does anyone else have this problem and a possible solution?
Internet explorer rejects sessions from domains with an underscore it. This is a known issue.
Please see here: Issue with Session and Cookie in Internet Explorer for websites containing underscore
And also:
http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx
Probably you have not set the _token parameter in your request to the server or you have put it in somewhere incorrect.

Laravel: Getting plain text while using Form class

I am a beginner of laravel. I have just installed "illuminate/html": "~5.0" with the help of link http://www.ekoim.com/blog/fix-class-form-html-not-found-laravel-5/
Now when I tried using of {{ Form::open() }} {{ Form::close() }} in view file. It is returning form in plain text format to browser.
<form method="POST" action="http://project.dev" accept-charset="UTF-8"><input name="_token" type="hidden" value="qNbi3DD1YGQncOv3PRBWl1l6BxVViWZnleVAmniS"></form>
I am attaching screenshot here.
Is there anything I forgot to do in order to this code to work?
Unlike Laravel 4, in Laravel 5 the content between {{ }} is now escaped by default. To output unescaped content you need to use {!! !!}. So in your case:
{!! Form::open() !!} {!! Form::close() !!}
Read more in the Laravel Templates Docs.

Resources