Method Not Allowed HTTP Exception in Laravel 5.4 - laravel

I have a form with two text fields and two buttons (edit and delete), when I press edit button, it works fine but when I press delete button, it gives the 'MethodNotAllowedHttp' exception. My code is as follows:
<form action="/laboratory/doctors/update" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
My Routes are as follows:
Route::post('/doctors/update', 'DoctorsController#update');
Route::delete('/doctors/{doctor}/delete', 'DoctorsController#destroy');
Any help is appreciated.

You have created route of http verbs as delete type and trying to get of type get. So you can change it as get instead delete
Route::get('/laboratory/doctors/{doctor}/delete', 'DoctorsController#destroy');
Also there is other way to make http verb as DELETE but using
Another form tag outside of current form tag.
Or use ajax to make it delete type

Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
DELETE
{{ Form::open(['url' => '/laboratory/doctors/{{ $doctor->id }}/delete', 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn btn-danger']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding {doctor} of the delete post so you dont have multiple html forms in your code.

#if(isset($doctor))
<form action="/laboratory/doctors/{{$doctor->id}}/delete" method="POST">
#else
<form action="/laboratory/doctors/update" method="POST">
#endif
{{ csrf_field() }}
#if(isset($doctor))
{{ method_field('DELETE') }}
#endif
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
The problem is in your action.. when you click delete button, the button run the destroy function, but the form run the update function. So, you have to separate the form action, or you can delete it by using ajax

Related

419 Sorry, your session has expired. Please refresh and try again.This error appears when i submit the form

I got this error even when I give {{ csrf_field() }} during form submission.
What could be the issue?
<form action="{{ route('e-account-post') }}" method="POST" id="e-account" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="form-group col-lg-6 col-md-6 col-xs-12">
<div class="field-label">Full Name <span class="required">*</span></div>
<input type="text" class="form-control" name="full_name" value="" placeholder="Full Name">
</div>
<div class="col-12 text-center">
<button class="btn credit-btn mt-30" type="submit">Send</button>
</div>
</form>
below is route
Route::post('/e-account','FrontController#e_account_action')->name('e-account-post');
below is controller
public function e_account_action(Request $request)
{
dd($request->all());
}
Laravel 5.7 and the project is live.
There is no problem in local. I setup the project in live mode through GitLab.

laravel Route::resource not working for delete

Laravel Route::resource('countries', 'CountriesController'); now working for DELETE
Working only as (separately)Route::delete('/countries/{country}/delete', 'CountriesController#destroy');
<div class="row">
<div class="col-12">
<h1>Details for {{ $country->countryName }}</h1>
<p>Edit</p>
<form action="/countries/{{ $country->id }}/delete" method="post">
<input name="_method" type="hidden" value="DELETE">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
</div>
</div>
not working, I'm stuck at url:
http://192.168.1.7:8000/countries/6/delete
saying '404|not found'
You don't need to append /delete in the URL.
Try this:
<form action="/countries/{{ $country->id }}" method="post">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
As mentioned by #nakov, you can also remove the hidden input field. The blade directive #method('DELETE') is sufficient to do the job.
Hope it helps!

Missing required parameters for route (Laravel 5.8)

I'm trying to recreate a blog application form old laravel version in new version (5.8). In old version, I used laravel collective for forms, and my edit post form looks like this:
#extends('layouts.app')
#section('content')
<h1>Edit Post</h1>
{!! Form::open(['action' => ['PostsController#update', $post->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<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, ['class' => 'form-control', 'placeholder' => 'Body Text'])}}
</div>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#endsection
Now I'm trying to recreate the same form without laravel collective forms because it seems to be deprecated.
This is my attempt at recreating this form:
#extends('layouts.app')
#section('content')
<h1>Edit post</h1>
<form action="{{ route('posts.update'), $post->id }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title" value={{$post->title}}>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body" value={{$post->body}}></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
#endsection
I'm getting the following error:
Missing required parameters for [Route: posts.update] [URI: posts/{post}]. (View: C:\xampp\htdocs\blog\resources\views\posts\edit.blade.php)
Looks like I'm not sending the id parameter correctly.
Also,how can I recreate this part: {{Form::hidden('_method', 'PUT')}} in plain HTML?
You can recreate the method input with this:
#method('PUT')
You need to put the $post->id inside the route() and use the Form method Spoofing (#method('PUT')). Try this:
#extends('layouts.app')
#section('content')
<h1>Edit post</h1>
<form action="{{ route('posts.update', $post->id) }}" method="POST">
#csrf
#method('PUT')
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title" value={{$post->title}}>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body" value={{$post->body}}></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
#endsection
Your error is caused by $post->id being outside of the route() function.
Change:
route('posts.update'), $post->id
to:
route('posts.update', $post)
As for the second question, this is how Form::hidden('_method', 'PUT') is rendered as HTML:
<input type="hidden" name="_method" value="PUT">
But you can use #method if you prefer a shorter way of writing it:
<form action="{{ route('posts.update', $post) }}" method="POST">
#method('PUT')
#csrf
...
</form>

Laravel 5.4 - Updating a resource

I'm building a blog post to learn Laravel 5.4 and am struggling to find any examples of how to update a Post anywhere.
My form is as follows
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
My Routes are as follows
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::patch('/posts/{post}', 'PostsController#update');
And my controller methods are
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post )
{
Post::where('id', $post)->update($request->all());
return redirect('home');
}
I get a MethodNotAllowedHTTPException error but am not sure which part / parts of this I am getting wrong.
I'm assuming it must be the point at which I'm using the PATCH function, or potentially just the way I'm mass-assigning the new values. Any help would be greatly appreciated.
you should use
{{ method_field('PATCH') }}
as your form field
and change action to
/posts/{{ $post->id }}
like this:
<form method="POST" action="/posts/{{ $post->id }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
There are a couple of things you have missed out.
Firstly, as #Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET and POST methods:
<input type="hidden" name="_method" value="PATCH">
or
{{ method_field('PATCH') }}
https://laravel.com/docs/5.4/routing#form-method-spoofing
Then you will also need to omit the "edit" uri in your forms action:
<form method="POST" action="/posts/{{ $post->id }}">
or
<form method="POST" action="{{ url('posts/' . $post->id) }}">
https://laravel.com/docs/5.4/controllers#resource-controllers
(scroll down a little bit to the Actions Handled By Resource Controller section)
You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10
Hope this helps!
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.

Destroy - Laravel 5.2

Do not delete that line of mysql. What is the problem ? D
Controller
public function destroy($id) {
$seriale = Serial::find($id);
$seriale->delete();
return redirect()->route('admin.seriale.index');
}
View
<form action="{{ route('admin.seriale.destroy',$seriale->id) }}" method="DELETE" role="form">
<button type="submit" class="btn btn-danger">Sterge Serial</button>
</form>
Fix Your form with this notation.
This helper adds special hidden fields to implement DELETE request.
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.seriale.destroy', $seriale->id] ]) }}
<button type="submit" class="btn btn-danger">Sterge Serial</button>
{{ Form::close() }}
Read this question and answers: https://laracasts.com/index.php/discuss/channels/general-discussion/how-to-updatedelete-using-forms-and-restful-controllers?page=1
You can not send DELETE requests easily. Browsers only understand GET and POST. You have to use a magic field to tell Laravel that this is a DELETE request:
<form action="{{ route('admin.seriale.destroy',$seriale->id) }}" method="POST" role="form">
{{ csrf_field() }}
<!-- <input type="hidden" name="_method" value="DELETE"> -->
{{ method_field('delete') }} <!-- helper functions in laravel are awesome -->
<button type="submit" class="btn btn-danger">Sterge Serial</button>
</form>
I also added the csrf field to the form, you might need it.

Resources