Submitting Data, Error: Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message - laravel

<form action="upload_creation" method="post">
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
this is my code for form, i want to submit a file with button
public function upload_creation(Request $request){
$input = $request->all();
$creation = $this->creationRepository->create($input);
foreach($request->file('direktori_gambar') as $image)
{
$name=time().$image->getClientOriginalName();
$image->move(public_path().'/public/img', $name);
$input['pictureName']=$image->getClientOriginalName();
$input['pictureFile']='/public/img/'.$name;
$mediaUkm = $this->creationPictRepository->create($input);
}
return view('webgallery.desktugas')->with($this->data);
this is the controller i referred to in form Action
after i click the submit button, it came up as no message error on laravel
any idea how to fix this?

Which method do you use in your <form> in your template?
If the route is post() (like it is in your routes) then you also need POST as method in your form.
If you have POST as method in the <form>-Tag, look if you have a hidden-input-field called _method.
More details here
https://laravel.com/docs/5.5/routing#form-method-spoofing

this is your route in web.php locatated in routes/ folder.
first create route in web.php
web.php
Route::post('upload-creation', 'CreationController#newCreation')->name('upload-creation');
And in form use route name to give action on file submit
<form action="{{route('upload-creation')}}" method="post">
{{ csrf_field() }}
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>

use enctype="multipart/form-data" in your form tag
example:
also don't forget to add {{ csrf_field() }}

You need to add a hidden input to your form to include your csrf token.
Laravel 5.6, 5.7
<form action="upload_creation" method="post">
#csrf
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
For reference visit the Documentation.
For older versions of laravel, the syntax is slightly different:
{{ csrf_field() }}

you can use Form Helper using Laravel Collectives
Begin by installing this package through Composer. Run the following from the Terminal:
composer require "laravelcollective/html":"^5.2.0"
Next, add your new provider to the providers' array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
Opening & Closing Form
{{ Form::open(['url' => route('upload-creation')]) }}
// default method is post, you can change by adding **method** key
{{ Form::close() }}
it will automatically generate the #csrf code which will be hidden but will be available during request submission

Related

DELETE method is not supported for this route. Supported methods: GET, HEAD, POST

I'm using laravel 9.x
my route is
Route::middleware('verified')->group(function (){
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::resource('kullanicilar', UserController::class);
});
and my controller has destroy methods
public function destroy($id)
{
echo 'destroy'.$id;
//User::find($id)->delete();
//return redirect()->route('kullanicilar.index')
// ->with('success','Kullanıcı başarı ile silindi.');
}
and my user_index.blade.php
<form method="POST" aciton="{{ route('kullanicilar.destroy',$user->id) }}" style="display:inline">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></button>
</form>
even though everything seems to comply with the rules, I'm getting this error.
You have a typo in the action element causing the form to be posted back to the same route as the original page;
<form method="POST" aciton="{{ route('kullanicilar.destroy',$user->id) }}"
note action is misspelled
Also, as you are using resource controller, you should accept the model in the destroy method.
Use Route::list to check what your controller should accept
action NOT aciton in Your Form Ex :
<form method="POST" action="{{ route('kullanicilar.destroy',$user->id) }}"
style="display:inline">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></button>
</form>
I found the solution by overriding the destroy method with get on the web.php route. it's working for me for now.
such as
//this should be at the top
Route::get('kullanicilar/remove/{id}', [UserController::class,'destroy'])->name('kullanicilar.remove');
Route::resource('kullanicilar', UserController::class);
and change my user_index.blade.php
<i class="fa fa-times"></i>
it works.

Element not getting updated

well.
I've been trying to update an element of a post on my project. Thing is that I've followed the same process I've made with another thing and now It's not working on this one... I really don't know why.
This is inside the controller:
public function approve($id){
$id = Vulnerability::where('id', $id)->first();
$status = $id->status;
$data['verified'] = "Approved";
$id->update($data);
return redirect::back();
}
the status thing is not getting updated... what should i do? I'm sure the code is right because i used the same piece for another feature... but... now it's not working...
this is the form:
<form method="POST" action="/bounty/accept/{{ $id->id }}">
<input type="hidden" name="_method" value="patch">
{{ csrf_field() }}
<div class="col-lg-12 col-md-6 col-sm-12 col-xs-12">
<button class="btn btn-primary btn-lg full-width" type="submit" id="markSolved">Approve (please talk with the user by starting a thread bellow)</button>
</div>
</form>
this is the route:
Route::patch('/bounty/accept/{id}', [
'uses' => 'BountyController#approve',
])->middleware('auth');
It's correct... as far as i know...

Laravel 5.2 - Form do not submit

Sorry, i'm very new to Laravel, i have this form:
<form action="/register" method="post">
{!! csrf_field() !!}
....
<div class="form-group">
<button type="button" class="btn btn-default">Register</button>
</div>
</form>
And the route is:
Route::get('/register', 'Auth\AuthController#getRegister');
Route::post('/register', 'Auth\AuthController#postRegister');
When i click on Register button nothing happen... the form do not submit, no errors etc.
Can you help me ?
Change this
<button type="button" class="btn btn-default">Register</button>
to
<button type="submit" class="btn btn-default">Register</button>
and always use dynamic url so that when you deploy your app to server,then you get no error(s)
action="{{ url('/register') }}
Change the type of the button:
<button type="submit"

Laravel delete method not working with DELETE verb

In my routes file, I have
Route::delete('events/{events}', ['as' => 'events_delete', 'uses' => 'Admin\EventsController#destroy'] );
In my view file I have
<em class="fa fa-trash"></em>
This does not work. When I change the route to
Route::get('events/{events}', ['as' => 'events_delete', 'uses' => 'Admin\EventsController#destroy'] );
it does work. However I don't like the idea of using a GET verb to delete items instead of the DELETE verb. It feels like a trick...
How can I change the form code to make sure it sends a DELETE verb?
Solution 1 (from TheFallen): with DELETE VERB in routes file
<form action="{!! route('events_delete', ['id' => $event->id ]) !!}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger btn-sm" type="submit"><em class="fa fa-trash"></em></button>
</form>
Solution 2: with GET VERB in routes file
<em class="fa fa-trash"></em>
You have to make a delete request to use the route this way, which you can do with a form, otherwise with the anchor you're making a get request.
If you already don't have the laravelcollective/html package install it from composer to use the forms facade. Then you can make the request like this:
{!! Form::open(['method' => 'DELETE', 'route' => $yourRoute]) !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
EDIT:
Without the forms facade:
<form action="{{ $yourRoute }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger btn-sm" type="submit"><em class="fa fa-trash"></em></button>
</form>
That will produce a GET request, therefore it will not match Route::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.
Refer: https://laravel.com/docs/master/routing#form-method-spoofing
To call the delete route, you have to implement using jquery
<a eventid="{{$event->id}}" href="#" type="button" class="btn btn-sm btn-danger"><em class="fa fa-trash"></em></a>
$(document).on("click",".anchorclass",function(e){
e.preventDefault();
if(!confirm("Are you sure?")) return;
$.ajax({
type: "DELETE",
url: 'events/'+$(this).attr("eventid"),
success: function(data) {
//Process results
}
});
});

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