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');
Related
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
I have a form in my Laravel 5.7 application to allow a user to upload a CSV file for importing data. It has been working for a few weeks. However, suddenly it started returning 404 errors for all POST requests where the form had multipart/form-data as its enctype. The strange thing is that when I change it to URLEncoded* there is no 404.
I have tried several things.
Changing the route name.
Checking php artisan route:list output to verify routes exist.
Accessing the route via GET method, and I get method not allowed exception.
Clearing the cache.
Blade Form
<form action="/import/createParts/upload/" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
#csrf
<button type="submit">Upload</button>
</form>
Routes
Route::prefix('import')->group(function () {
Route::get('/createParts', 'Import\CreatePartsController#index');
Route::post('/createParts/upload', 'Import\CreatePartsController#upload');
});
Ideally, this should pass the form over to the function, and another process happens.
Make sure you set a name for your route, it's useful.
Routes
Route::prefix('import')->group(function () {
Route::post('createparts/upload', 'Import\CreatePartsController#upload')
->name('import.createparts.upload');
});
Blade
<form method="post" action="{{ route('import.createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<button type="submit">Upload</button>
</form>
Named route concept is much more easy way handling the routes.
In web.php
Route::post('import/createParts/upload', 'Import\CreatePartsController#upload')
->name('createparts.upload');
In blade
<form method="post" action="{{ route('createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<input type="submit" value="Upload"/>
</form>
I'm new to laravel and still learning about this framework.
I already found some questions on stackoverflow but it still didn't work out for me.
My problem is:
I got this
localhost/codehub/public/users/create
and the route:
Route::get('users/create',['uses' => 'UserController#create']);
Inside the page there's some form like this:
so when I click create button it is supposed to route it into store function in the user controller
Route::post('users',['uses' => 'UserController#store']);
public function store(Request $request)
{
return $request->all();
}
so the problem is when I click that create button it always redirects me to localhost/users and because of that, I can't process my store function.
Any advice?
this is my form code:
<form method="post" action="/users">
<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit" value="Create">
</form>
The problem may be because of relative path in form action.
You should always use named routes which allow the convenient way of generation of URLs or redirects for specific routes.
So you can change your route as:
Route::post('users', 'UserController#store')->name('users.create');
And in form you can write as:
<form method="post" action="{{ route('users.create') }}">
To make my life simpler in a huge website, can I do this?
Route::get('view/{id}', 'PostController#show')->name('post');
Route::delete('view/{id}', 'PostController#delete')->name('post');
Route::post('view/{id}', 'PostController#save')->name('post');
And then in my form, I can do this.
<!-- Delete Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete</button>
</form>
<!-- Edit Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="PATCH">
<button type="submit">Edit</button>
</form>
<!-- Etc -->
Can I do this? Is this recommended?
Yes, in fact you can use the following to keep your route file clean: https://laravel.com/docs/5.2/controllers#restful-naming-resource-route-parameters
Route::resource('foobar', 'FooBarController');
This will auto generate RESTful routes for you:
if you run php artisan route:list you can see all the HTTP routes.
I have form like this
<form action="{{ Request::root() }}/articles/update/" method="post">
<input type="hidden" name="id" value="{{ $article->id }}" />
<input type="submit" name="submit" value="Submit" />
</form>
And route like this
Route::post('articles/update', array('as' => 'articleUpdate', 'uses' => 'ArticlesController#update'));
But when I submit the form, I get MethodNotAllowedHttpException. In error report I can see that request method is GET. I have also tried using caps for method method="POST" but it didn't work.
Any ideas?
What does FireBug/Web console inspector show you? is the form being sent via GET or POST, any redirects?
Seems a redirection problem to me, after reaching the server Laravel redirects to the URL the form sent the post request.
you must use put method here. Form change like this
{{Form::open(array('url'=>'/articles/update','method' => 'PUT'))}}
Routes like this
Route::put('/articles/update','ArticlesController#update');