Laravel method not allowed exception - php

I can't seem to find out why does my submit give me back the MethodNotAllowedException. Shortened...here is the form:
<form role="form" id="tryitForm" class="form-horizontal" enctype="multipart/form-data"
method="POST" action="{{route('user.update', Auth::user()->id)}}">
<input type="submit" class="btn btn-primary" name="save" value="Update"/>
And I have route set up as:
Route::resource('user', 'UserController');

You need to add PUT method into your form.
<input name="_method" value="PUT" type="hidden">
<input type="hidden" value="{{ csrf_token() }}" name="_token">

Related

Laravel 6 404 Not found but route exists

Getting 404 Not found though route exists, the following codes worked perfectly on Laravel 8 but on 6 produces 404.
Route:
// Content Packs
Route::delete('content-packs/destroy', 'ContentPacksController#massDestroy')->name('content-packs.massDestroy');
Route::patch('content-packs/{content-pack}/clone_pack', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');
Route::resource('content-packs', 'ContentPacksController');
Button:
<form action="{{ route('admin.content-packs.clone_pack', $contentPack->id) }}" method="POST" onsubmit="return confirm('{{ trans('cruds.contentPack.clone_confirmation') }}');" style="display: inline-block;">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-warning" value="{{ trans('cruds.contentPack.clone') }}">
</form>
Controller method:
public function clone_pack(Request $request, ContentPack $contentPack)
{
$contentPack = ContentPack::where('id', $request->id)->first();
$newPack = $contentPack->replicate();
$newPack->created_at = Carbon::now();
$newPack->save();
return back();
}
What am I missing?
Changing route to this fixed the issue:
Route::get('content-packs/content-pack/{id}', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');

The GET method is not supported for this route. Supported methods: DELETE

I got this error when I try to remove subcategories by however I did use the delete method.
blade.php:-
<form action="{{ url('sub_category/delete',$item->id) }}" method="DELETE">
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
my web
Route::DELETE('/sub_category/delete/{id}','SubcategoryController#destroy');
controller:-
public function destroy($id)
{
$subcategory= Subcategory::where('id',$id)->delete();
return redirect()->route('cars.index');
// ->with('success','Car deleted successfully');
}
Your form should be in the following way
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
<input name="_method" type="hidden" value="DELETE">
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
DELETE,PUT,PATCH,HEAD methods should be defined as follows
#method('DELETE') #method('PUT') ...
Laravel routing
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
try like this one:
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
{{method_field('DELETE')}}
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
your route:
Route::delete('/sub_category/delete/{id}','SubcategoryController#destroy');

post route in Laravel

I have done everything the right way but my submit button doesnt do anything and I dont know why....
Here is my view
<form action="{{ route('importUser') }}" method="POST" enctype="multipart/form-data">
#csrf
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
Here is my controller
function importUser(Request $request)
{
#code...
}
and my route
Route::POST('ImportUsersFile', 'ExcelUserController#importUser')->name('importUser')->middleware('Admin');
Apparently, the flow dont get in the function import user. I tried to dd into it but nothing happend!
According to an error message you provided in a comment, try this:
php artisan key:generate
Try using url intead of route
<form action="{{ url('ImportUsersFile') }}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
And in your routes:
Route::post('ImportUsersFile', ['uses' => 'ExcelUserController#importUser', 'as' => 'importUser']);

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Delete a record gave me NotFoundException in laravel 5

How can I create multiple requests for the same route like below.
Route.php
Route::get('/home', 'HomeController#index');//->middleware('auth');
Route::get('/home/{$user}','HomeController#showStudent');
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
the form was working fine until I have added the delete request. In my blade template I have code something like this.
home.blade.php
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
{{ csrf_field() }}
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
I believe because of the same routes it's showing NotFoundHTTPException.
On one route /home I am trying to Add, Show, Edit and Delete a record with different buttons.
Thanks in Advance.
You could add a form and use Laravel's Form Method Spoofing
<input type="hidden" name="_method" value="DELETE">
See more here...http://laravel.com/docs/master/routing#form-method-spoofing
Try as below....
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
1) Change you route from:
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
To:
Route::get('/delete/{$Id}','HomeController#deleteStudent')->name('delete');
2) change you form tag from:
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
To:
<form class="" role="form" method="get" action="route('delete', ['id' => $student->id])">
HTML forms doesn't support methods other than get and post. If you need to simulate it, include a hidden input to simulate delete:
<input name="_method" type="hidden" value="DELETE">
Then in your code, update it to:
<form class="" role="form" method="POST" action="/home/{{$student->id}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
Reference:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
http://laraveldaily.com/theres-no-putpatchdelete-method-or-how-to-build-a-laravel-form-manually/

Categories