Laravel same URL with 2 method - laravel

I am using same URL (Get and Post ) but on submit button it using the get method not the post .
my is below :
<form action="{{route('document.update', $document->id)}}" method="POST"
enctype="multipart/form-data" class="form-action">
<input type="hidden" name="flag" class="flag" value="0">
#csrf
#method('PATCH')
<button type="submit" class="btn btn-md btn-primary mr-1 btn-submit"
data-text="{{ __('save-all-changes') }}" name="post_action"
role="button" value="post_comment">{{ __('post-a-comment') }}
I need to take the post not the get , any idea?

I found the problem , since i disabled all input using
$("input").prop('disabled', true);
this has disable the _token and _method input , Change the Jquery sentence to be :
$("input").not( ':input[type=hidden]').prop('disabled', true);
this fix the problem .

Related

pass input value to url delete method laravel

i have form in my delete modal, and i want to pass hidden input to url method delete/destroy
this is my delete modal :
<form action="{{url('/pages').'/' ... }}" method="POST" enctype="multipart/form-data">
{{ method_field('DELETE') }}
{{csrf_field()}}
<div class="form-group">
<label for="">Are you sure to delete this Page ?</label>
<input type="text" class="form-control" id="page_id" style="display: none;">
</div>
<button type="submit" class="btn btn-danger" id="save">Delete</button>
<button type="button" class="btn btn-link" data-dismiss="modal">{{__('Close')}}</button>
</form>
how to append input value(page_id) to form action ?
so i can use that page_id in form action :
<form action="{{url('/pages').'/'page_id}}" method="POST" enctype="multipart/form-data">
can some one help me ? thanks before :)
You have to add a new attribute on each delete button contains the id.
please check the following code
$('.delete-button').click(function(){
var pageId = $(this).data('page-id');
$('.page_id').html(pageId);
$('#modal-form').attr('action', deleteUrl+pageId);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
page 1
page 2
page 3
<form action="#" id="modal-form" method="POST" enctype="multipart/form-data">
<label for="">Are you sure to delete this Page <span class="page_id"></span>?</label>
</form>
<script>
/* window.deleteUrl = "{{url('/pages').'/'}}"; // save globally */
window.deleteUrl = "http://test.com/"; // temporary
</script>
If you are using boostrap modal then you have to pass data from that button something like this,
<button id="submit-btn" data-action="{{route('test')}}">Submit</button>
In JS,
replace modal's action with data-action.
Something like this,
$('#form-id').val($('#submit-btn').data('action))
And if you are not using modal then just store that id in variable and pass in route directly.
Hope it helps :)

Laravel | Form not submitting to route given in action [SOLVED]

I'm trying to submit a form to the right route using the action:
<form action="{{ route('document.destroy', $d->id) }}" method="POST" style="display: inline;">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<span type="text" value="" class="btn btn-success" readonly="readonly">
{{ $d->file_name }} ({{ $d->file_size }}) <i class="fa fa-times"></i>
</span>
<button class="btn btn-xs btn-default" type="submit" data-toggle="tooltip" title="Verwijder" onclick="return confirm('Weet je zeker dat je dit document wilt verwijderen')"><i class="fa fa-times"></i></button>
</form>
But yet it links to a different controller. What am I doing wrong?
I also specified the controller in the routes file:
Route::resource('document', 'DocumentController');
To add more context: this view gets passed from a different controller than I want to use for the DELETE function.
EDIT
I used a form inside another form, my stupid head didn't see it.
for deleting you can try this
<form action="{{action('DocumentController#destroy', $d->id)}}" method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
and Destroy method
public function destroy($id)
{
$doc= Document::find($id);
$doc->delete();
return redirect('/home')->with('success', 'Document has been deleted!!');
}

Update method in html form in laravel

I'm trying to update inputs using html form in laravel:
<form action="{!! route('users.update',['id' => $users->id]) !!}" method="post">
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">customer_name</label>
<div class="col-sm-10">
<input value="{{$name}}" class="form-control form-control-lg" placeholder="col-form-label-lg">
</div>
<button type="submit" class="btn btn-primary btn-lg" > Edit</button>
</form>
Everything in the controller work perfectly however in the view page I received this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
No message
What am I doing wrong?
Please correct your route as POST like:
Route::post('update/{id}', 'YourController#update')->name('users.update');
You need to spoof the method as you are using to post the data. 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. The #method Blade directive can create this field for you like this:
<form action="/foo/bar" method="POST">
#method('PUT') //add this to your form
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('patch')}} //add this to your form
</form>
You need to put #csrf and #method('PATCH') inside your form view.
I have the same problem and I had it worked after adding some lines in my code:
Just use $users->id instead of doing it as array ['id' => $users->id]
Use csrf and spoof the method by adding #method('PUT')
Your code should look like this:
<form action="{!! route('users.update', $users->id) !!}" method="post">
#csrf
<!--Some fields-->
#method('PUT')
</form>

laravel delete method not working

I've made a small laravel project but the delete method is nog working:
I use a resource controller
my route is :
Route::resource('roles','Admin\RoleController');
in my view I have
<form action="{{route('roles.destroy',$role->id)}}" style="display:inline">
#method('delete')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
But when i click the button it will show me the role ( = get method of the resource )
What am I doing wrong ?
If you're using Laravel 5.1 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
If you're using Laravel 5.6 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
You can read more about method spoofing in Laravel Documentation.
Check Laravel's documentation.
Did you try adding method="POST" to the form?

Method Not Allowed HTTP Exception in Laravel 5.4

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

Resources