Laravel 5.8: POST Route Does Not Seem To Be Working - php

I'm using Laravel 5.8 for my project and in this project, I have added this route:
Route::post('course_admin/{id}','CourseController#AcceptWalletCourse')->name('accept.walletCourse');
And here is the form:
<form method="POST" action="{{ route('accept.walletCourse',['id'=>last(request()->segments())]) }}">
#csrf
Are you sure?
<button type="submit" class="btn btn-success">Yes</button>
</form>
And at the Controller, I added this:
dd($request);
But I don't know why nothing appears as result. I mean the page just refreshes without showing any error or any dd result.
So what's going wrong here? How can I solve this issue?

Try to simplify everything:
<form method="POST" action="{{ route('accept.walletCourse',['id'=> 12]) }}">
#csrf
Are you sure?
<button type="submit" class="btn btn-success">Yes</button>
</form>
In your controller:
public function AcceptWalletCourse($id)
{
dd($id); //should print 12
}
Once you are at this point, you can start adding validations, policies or dynamic parameters in your form (for instance last(request()->segments())) .
By proceeding step by step, you will quickly find where is the problem.

Try php artisan clear-compiled composer dump-autoload php artisan optimize

Related

NotFoundHttpException en laravel 5.2

i'm using laravel 5.2 and i'm trying to define routes, but i don't know why i get NotFoundHttpException when i try to do post.
<form action="POST" action="post_to_me">
<input type="text" name="name" >
<input type="submit">
</form>
this is my form, very simple.
route::post('post_to_me',function(){
echo "post";
});
here i define the route to post, but when i run tha app, NotFoundHttpException comes out. Does anyone know why?
I think you have missed adding the CSRF Token to your form. Try adding
// Vanilla PHP
<?php echo csrf_field(); ?>
or
// Blade Template Syntax
{{ csrf_field() }}
just below your form tag

how to solve Laravel routes conflict

this is a simple code to search in laravel.
the route "product" has no problem at all, but,
when I use the route "searchproduct", the url in the browser looks like this:
http://example.com/application/public/product/search?q=red+dead
so the application thinks that i'm trying to get the route "product" and send the parameter urlkey as "search?q=red+dead", which ofcourse throw an error.
View
<form method="GET" action="{{ route('searchproduct') }}" >
<input id="q" name="q" class="q" type="text" />
<button type="submit" id="submitButton" class="btn btn-primary">Go</button>
</form>
Routes
Route::get('product/{urlkey}','ProductController#index')->name('product');
Route::get('product/search/{q?}','ProductController#search')->name('searchproduct');
how to solve this issue please?
define routes this order
Route::get('product/search/{q?}','ProductController#search')->name('searchproduct');
Route::get('product/{urlkey}','ProductController#index')->name('product');

Laravel 5.4 "MethodNotAllowedHttpException" (Basic Task List)

i was trying to follow this tutorial on Laravel, even though most of the code & directories there were outdated, i managed to make the "Add Task" and also The Form to show up after a few errors.
now the problem, the delete button.
when i click on it, it shows a "MethodNotAllowedHttpException".
i changed the source code to match the newest version of Laravel.
my form (current version) :
<form action="{{ url('/task/'.$task->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
my route :
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
i've been trying to fix this for 4 hours now, changing the methods of my route and form; but to no avail.
this is my first question on this site, sorry if there's something wrong in this question.
thanks~
edit:
to further help the effort, here's the complete error log
Error log, in Google Chrome
Change
<form action="{{ url('task/'.$task->id) }}" method="DELETE">
to
<form action="{{ url('task/'.$task->id) }}" method="POST">
Because form method DELETE does not exist, Laravel just "overwrites" this method with the hidden input "method" (you can place this input using "{{ method_field('DELETE') }}").
Form dosent support method Delete or Put ... It support only get and post methods, if You want implement delete in laravel this article will help you
link
Answer :
Turns out the problem lies in the Database-table itself, let me explain :
i was referring to a 'tasks' table that is referred as 'task' in code (no problem)
BUT, i was referring to a column called "ID" in my table as "id" in my code, creating the error (a rookie mistake).
thanks to #Autista_z for the pointer, and everyone else below for the guidance!

form update with file upload laravel

I'm having a bit of an issue when it comes to updating a form and and having an file input. Here is what I am working with.
I have a form in laravel 5.1 which has a post method and a hidden 'Patch' method. This works as is should updating the fields that are in the form. However, when it introduce:
<input type="file" id="profile_picture" name="image_url" />
into the form, i get a:
MethodNotAllowedHttpException in RouteCollection.php line 218:
laravel error. I have tried changing the
<input type='hidden' name='_method' value='PATCH'>
to PUT and it still doesnt like it.
My form looks like this:
<form action='{{url("profiles/$user->id")}}' method="post" class="form-horizontal" enctype="multipart/form-data">
route resource looks like this:
Route::resource('profiles', 'ProfilesController');
I can't figure out what I am missing here...Any help is much appreciated.
I believe it has to do with the exact route you are typing out in the "action" parameter matching up with the profile controller's update method.
Try changing
action'{{url("profiles/$user->id")}}'
to
action='{{ route("profiles.update", $user->id) }}'
Additionally, you could use the Laravel Collective HTML package to simply opening and closing of forms.
Also for POST Request types, you need to send the CSRF token along with your form data. If you are using laravel blade template in your view, you may use
{{ csrf_field() }}
which translates to
<input type="hidden" name="_token" value={{ csrf_token() }}
Please refer the documentation for this.

Delete handled by resource controller doesn't work - Laravel 5.3

I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>

Categories