MethodNotAllowedHttpException in RouteCollection - laravel

I have the MethodNotAllowedHttpException error when I try to update a data
I try to change the Form::model route to PUT and PATCH
Here's my form::model:
{!! Form::model($mission, ['route' => ['missions.update', $mission->id_missions], 'method' => 'PUT', 'class' => 'form-horizontal panel']) !!}
And here's my route:
Route::resource('missions', 'MissionsController');
I got the error mentionned above
Can somebody help me please ?

Maybe you forgot to spoof the PUT method in your form, you can do that by using blade's #method('PUT').
This is how you can implement it :
<form action="/foo/bar" method="POST">
#method('PUT')
</form>
So try also changing the form's method to POST when you use the Form::model helper because HTML forms can only be sent by GET or POST methods hence why one has to spoof the other CRUD methods.
You can read more about that here.

Related

Route is not working, it responds with "404 NOT FOUND"

I have a route which has two parameters.
Route::delete('/org/{org-id}/delete/user/{user-id}', 'App\Http\Controllers\OrganizationController#deleteUserFromOrg')
->name('delete-user-from-org');
i call it in view
<td><button class="btn btn-danger">Удалить</button></td>
i think this should work because it substitutes the data correctly and redirects to the page http://localhost:8000/org/11/delete/user/2. REturn 404 error.
It's does not see the controller, I tried some function in the route itself, this does not work either.
Route::delete('/org/{org-id}/delete/user/{user-id}', 'App\Http\Controllers\OrganizationController#deleteUserFromOrg')
->name('delete-user-from-org');
look at the Route::delete() part. In order to invoke that route i.e. delete-user-from-org, you will require a DELETE request method (or create a form with a hidden input via #method('delete')
When you create a link with <a></a>, the request will be a GET (by default unless manipulated by js) which is why you are getting a 404 Not Found Error. You have two solutions.
First Solution (via form):
<form action="{{route('delete-user-from-org',['org-id'=>$data->id, 'user-id' => $el->id ])}}" method="POST">
#method('DELETE')
...
</form>
Second solution (via ajax):
fetch("{{route('delete-user-from-org',['org-id'=>$data->id, 'user-id' => $el->id ])}}", {
method: 'DELETE',
})
.then(res => res.text()) // or res.json()
.then(res => console.log(res))
Third Solution (change the method): Not recommended. Avoid it.
Route::get('/org/{org-id}/delete/user/{user-id}', 'App\Http\Controllers\OrganizationController#deleteUserFromOrg')
->name('delete-user-from-org');
from Route::delete() to Route::get()
Route params should consist of alphabetic characters or _
Please fix your route params name
For example:
Route::delete('/org/{org_id}/delete/user/{user_id}', 'App\Http\Controllers\OrganizationController#deleteUserFromOrg')
->name('delete-user-from-org');
And your view blade
<td><button class="btn btn-danger">Удалить</button></td>

Laravel named route with {parametr}

Route::match(['patch','put'],'/edit/{id}', 'TestController#update')->name('update');
using route() helper in form action I expected to see
https://example.com/edit/1
And what I get using {{ route('update', $article->id) }} is https://example.com/edit?1
Any ideas how to resolve this?
Try passing the id in as an array:
route('update', ['id' => $article->id])
and make sure the form's method attribute is post as well as setting the correct _method value within the form:
<form action="{{ route('upate', ['id' => $article->id]) }}" method="post">
{{ method_field('patch') }}
</form>
I tried your example and it seems to work as expected. Going by the ? in the URL, my guess would be that it is a GET instead of POST in the form? Could you confirm that?

laravel upload file - Call to a member function getClientOriginalName

I have a strange problem today because the code that I will show you is to upload and move an image in a database + uploads directory in my laravel.
The last projects worked but today with laravel 5.4 the code doesn't work anymore and when I want to upload a new image I get an exception
with this line:
Call to a member function getClientOriginalName() on null
Line : $licencie_structure->lb_photo = $request->file('lb_photo')->getClientOriginalName();
Here my blade line to upload the file :
<div class="form-group">
<label>Select a picture : </label>
{!! Form::file('lb_photo' , ['class' => 'form-control', 'placeholder' => 'Photo']) !!}
</div>
Does someone have an idea why I get an exception : Call to a member function getClientOriginalName() on null?
Thanks a lot in advance friends!
This is returning null, meaning it's not in the request object.
$request->file('lb_photo')
Is this form actually sending the upload? Did you forget to add enctype='multipart/form-data' to the form? Is the name correct?
Check the output of $request->all().

Why does not work POST routing in Laravel

I use the standard authentication mechanism in Laravel.
Route path is:
Route::post('auth/register', 'Auth\AuthController#postRegister');
HTML form is:
<form method="POST" action="/auth/register">
When I submit form I get 404 error.
But path for GET methos works:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Use Laravel's helper function url() to generate an absolute URL. In your case the code would be:
<form method="POST" action="{{ url('auth/login') }}">
You could also check out the laravelcollective forms package. These classes were removed from the core after L4. This way you could build HTML forms using PHP only:
echo Form::open(['url' => 'auth/login', 'method' => 'post'])
you can use {{ URL::route('register') }} function in the action method. In This case the method will be:
action="{{ URL::route('register') }}"
and your route file will be:
`
Route::get('auth/register',array('as' =>'register' ,'uses' => 'Auth\AuthController#getRegister'));
Route::post('auth/register', array('as' =>'register' ,'uses' =>'Auth\AuthController#postRegister'));
`

How does Laravel handle PUT requests from browsers?

I know browsers only support POST and GET requests, and Laravel supports PUT requests using the following code:
<?= Form::open('/path/', 'PUT'); ?>
... form stuff ...
<?= Form::close(); ?>
This produces the following HTML
<form method="POST" action="http://example.com/home/" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT" />
... form stuff ...
</form>
How does the framework handle this? Does it capture the POST request before deciding which route to send the request off to? Does it use ajax to send an actual PUT to the framework?
It inserts a hidden field, and that field mentions it is a PUT or DELETE request
See here:
echo Form::open('user/profile', 'PUT');
results in:
<input type="hidden" name="_method" value="PUT">
Then it looks for _method when routing in the request.php core file (look for 'spoofing' in the code) - and if it detects it - will use that value to route to the correct restful controller.
It is still using "POST" to achieve this. There is no ajax used.
Laravel uses the symfony Http Foundation which checks for this _method variable and changes the request to either PUT or DELETE based on its contents. Yes, this happens before routing takes place.
You can also use an array within your form open like so:
{{ Form::open( array('route' => array('equipment.update', $item->id ),
'role' => 'form',
'method' => 'put')) }}
Simply change the method to what you want.
While a late answer, I feel it is important to add this for anyone else who finds this and can't get their API to work.
When using Laravel's resource routes like this:
Route::resource('myRoute','MyController');
It will expect a PUT in order to call the update() method. For this to work normally (outside of a form submission), you need to make sure you pass the ContentType as x-www-form-urlencoded. This is default for forms, but making requests with cURL or using a tool like Postman will not work unless you set this.
PUT usually refers to update request.
When you open a form inside laravel blade template using,
{{ Form::open('/path/', 'PUT') }}
It would create a hidden field inside the form as follows,
<input type="hidden" name="_method" value="PUT" />
In order for you to process the PUT request inside your controller, you would need to create a method with a put prefix,
for example, putMethodName()
so if you specify,
{{ Form::open('controller/methodName/', 'PUT') }}
inside Form:open. Then you would need to create a controller method as follows,
class Controller extends BaseController {
public function putMethodName()
{
// put - usual update code logic goes here
}
}

Resources