How to solve MethodNotAllowedHttpException in RouteCollection.php line 218:? - php

My view is like this :
#foreach($users as $user)
<tr>
<td>{!! $user->id !!}</td>
<td>{!! $user->username !!}</td>
<td>{!! $user->phone !!}</td>
<td>{!! $user->address !!}</td>
<td>
{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
<div class='btn-group'>
</i>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes\web.php is like this :
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
Route::resource('users', 'UserController');
My controller is like this :
public function destroy($id, $year)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index.year', ['year' => $year]));
}
$this->userRepository->delete($id);
Flash::success('User deleted successfully.');
return redirect(route('users.index.year', ['year' => $year]));
}
There is exist error like this :
MethodNotAllowedHttpException in RouteCollection.php line 218:
And the url looks like this : http://localhost/mysystem/public/users/2?2016
When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2016
Is there any people who can help me?

In your view you have declared the form method as DELETE; but in the routes\web.php file, you have declared the route as GET
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'get']
)!!}
Your routes/web.php should look like so,
Route::get('users/index/{year}', 'UserController#index')
->name('users.index.year');
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Route::resource('users', 'UserController', ['except' => ['index', 'destroy']]);
Keep your controller code as it is.
This should do the trick.
Just a suggestion
It's better not to keep delete operation on GET request. So change the DELETE route like so
Route::delete('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'delete']
)!!}

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">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

id is not declared in route and may be this is the cause, So change you route:
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
to this
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')->name('users.destroy.year');
Hope this solve this cause.

Related

Deleting or Updating posts in LARAVEL using controller

{!! Form::open(['action'=>['PostsController#update',$post->id],'method'=>'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title', $post->title,['class'=>'form-
control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body', $post->body,['id'=>'article
ckeditor','class'=>'form-control','placeholder'=>'Body Text'])}}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Update',['class'=>'btn btn-success'])}}
{!! Form::close() !!}
I am using this {{ Form::hidden('_method','PUT') }} to update my post because there is no other way. Is there any better way or not?
Here is my controller (postcontroller):
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required',
'body' => 'required'
]);
$post = Post::find($id);
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/posts')->with('success','Post Updated Sucessfully');
}
This is the default and correct way to use UPDATE form. You have to use POST in form method and PUT as request parameter (hidden) with form.
What you missed here is CSRF token in form, which can be used in multiple ways like:
{!! Form::token() !!}
{!! Form::hidden('_token', Session::token()) !!}
{!! csrf_field() !!}
#csrf <!-- since Laravel 5.6 -->
Similarly, you can use PUT as:
#method('PUT')

store in table, Laravel

I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!
WishlistController.php:
public function store($book_id)
{
$user_id=Auth::id();
$wishlist=new Wishlist;
$wishlist->book_id=$book_id;
$wishlist->user_id= $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id->id)
->with('success', 'Buch gewünscht');
The Model:
class Wishlist extends Model
{
public $table = 'wishlist';
public $fillable = ['book_id','user_id',];
the view.blade:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
<form action="someaction" method="POST">
<input type="hidden" name="book_id" value="{{$book->id}}"/>
</form>
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD.store', 'WishlistController#store');
When I check the table, nothing new is added.
Its frustrating :-(
Just change post to get in this line:
Route::post('wishlistCRUD.store', 'wishlistCRUD#store');
Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:
public function store(Request $request)
{
$user_id = Auth::id();
$book_id = $request->book_id;
$wishlist = new Wishlist;
$wishlist->book_id = $book_id;
$wishlist->user_id = $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id)
->with('success', 'Buch gewünscht');
}
Don't forget to add use Illuminate\Http\Request;
And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
{{ csrf_field() }}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
{!! Form::submit('wünschen') !!}
{!! Form::close() !!}
This should work.
You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.
in your web.php
Route::get('wishlistCRUD/book/{book_id}','WishlistController#get_book_by_id')->name('get_book_by_id');
Route::post('wishlistCRUD.store', 'WishlistController#store')->name('store');
In your blade template
<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>
<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>
in your controller
public function store(Request $request){
$bookID= $request->input('bookid');
$wishlist=new Wishlist();
$wishlist->book_id= $bookID
$wishlist->user_id= Auth::user()->id;
$wishlist->save();
return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}
public function get_book_by_id(Request $request,$book_id){
// find book by ID;
$book=Book::find($book_id);
// book found
if($book){
return view('book')->with('book',$book);
}else{
// book not found , redirect to 404 page or home page
return redirect('/');
}
}
In blade, change the
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
to a <button class="btn btn-primary" type="submit">wünschen</button>
Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}
You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>
Try this:
the view.blade:
{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
<button class="btn btn-primary" type="submit">wünschen</button>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD/store', 'WishlistController#store');

Laravel calling #show instead of #destroy

I have a problem with my routes in Laravel (v. 4.2)..
View:
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
Route:
Route::resource('admin/users', 'App\Controllers\Admin\UserIndexController');
Controller:
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
When the button is clicked though it always prints out "show". Why is this?
HTML forms cannot make PUT, PATCH, or DELETE requests so you need to spoof it with Laravel.
Add this to your form...
<input type="hidden" name="_method" value="delete" />
I believe this generally isn't an issue with newer versions of Laravel since the Form builder has been removed from the core and is now being managed by LaravelCollective which will handle adding this input automatically.
https://laravel.com/docs/4.2/html#opening-a-form
Edited Answer:
Routes.php
Route::resource('admin/users', 'UserIndexController');
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
I am getting "destroy" as output.

Laravel deleting a file NotFoundHttpException

this is my form in my view
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
controller in which i delete file from database and the original file
public function destroyFile($file_name, $id)
{
File::findOrFail($id)->delete();
$file_path = storage_path('documents').'/'.$file_name;
$destinationPath = $file_path; File::delete($file_path);
return redirect('/documents');
}
This is the route
Route::delete('documents/{file}/{id}','FilesController#destroyFile');
And when i press submit button I get NotFoundHttpException
Try to use this
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name, $file->id] ]) !!}
Actually, their answers are correct. You need the _method to be DELETE. When I am using this. Laravel do it for me.
Or you can put this on your form
<input type="hidden" name="_method" value="DELETE">
or
{!! Form::hidden('_method', 'DELETE') !!}
It is not possible to use this method with html forms in most browsers, most only support GET and POST.
So the reason for this request not working is because the browser sends this as a GET request, wich is the default.
GET, POST, PUT and DELETE are however supported in most major browsers when using XMLHttpRequests (ajax).
add {{ method_field('DELETE') }} to your form .
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{{ method_field('DELETE') }}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
The reason is that HTML forms does not support PUT, PATCH, DELETE actions. Basically you need to spoof them as described here. https://laravel.com/docs/5.2/routing#form-method-spoofing

delete function Laravel 5.2

I've been learning laravel 5.2 recently, and i've made a delete function which should delete records from my database but instead of deleteing the records it's adding a blank row into my database
This is the Route im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'destroy', 'edit', 'update', 'create']]);
This is the controller function i use for it
public function destroy(request $request , product $product)
{
$product->delete();
return redirect(Route('producten.index'));
}
This is the form i've made for it.
{{ Form::Open(['Route' => 'producten.destroy', $product], ['method' => 'delete']) }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
when i viewed the source-code it said it was using a POST method instead of a delete method, and also when i add($product) i got a blank page, also i found out that when i hit the submit button it goes to the store method i've made and i dont know why,
if u need more information just let me know and i'll add it in the question
route and method should be in the same array, not in two differents arrays.
{{ Form::Open(['method' => 'DELETE', 'route' => ['producten.destroy', $product]]) }}
{{ method_field('DELETE') }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
I think you have something wrong with form. Can you try with this:
<form action="{{ route('producten.destroy', ['product' => $product->id]) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Remove</button>
</form>

Categories