Thank you for the amazing tutorial. The test case "test_tours_list_filters_by_price_correctly" fails because we multiply the price values in TourController for filtering. So, we need to either increase the 'price' we pass to the factory by multiplying it by 100, or multiply the 'priceFrom' value from the query parameter by 100 before passing it.
There are a few packages for filtering and ordering, maybe would use them. Or move the logic to some TourService. But I don't see a big need in this specific case.
is there any other configuration need to be done in order to use form request to validate. my form request class does not seem to do the job. it redirect to home page on browser and in postman it returns a 200 status with no errors
That happens because the default response of FormRequest is a Redirect Back with errors, the errors are on the session and you can get them on the frontend via the session helper. If you want to return a JSON response error, you must set the Request Header "Accept" to "application/json". With that you tell Laravel that you want a Json response. If you want every response to be a json, like in a API, you must create a middleware to automaticly set this header.
Hello Povilas! I came across an issue when i was following the lesson. After putting validation in the code, i tried it in Postman. When provided correct query params - the result was as expected. But when i put sortOrder=random, i saw a default Laravel welcome page as a result. Same thing in browser. I've got puzzled for a few minutes. Why i don't see validation error!? When inspecting developer tools in the browser, i noticed that when you send request with wrong values - it responses with 302 (redirect) and then loads welcome page. Then i inspected Postman headers. Accept is set as '*/*' by default. I overrode it with 'application/json' and then i begun to see the correct error. Good. But.. why in the browser it does 302 and heads to welcome page instead? How can i get the expected validation error in json there !?
The test also failed: FAILED Tests\Feature\ToursListTest > tour list returns validation errors Expected response status code [422] but received 302. The following errors occurred during the last request: The date from field must be a valid date. At this point i don't know what to do.
@@LaravelDaily aah... yeah, you are right! I simply didn't notice you used `$response->getJson()` in the last one. All the rest were `$response->get()`. Thank you! My bad :)
I faced your exact issue with postman. Solution. Created a middleware with this line $request->headers->set('Accept', 'application/json'); then registered it in $middlewareAliases array in Kernel.php as 'forceJson' => \App\Http\Middleware\ForceJsonResponse::class. Then chained the middleware when making the request this way Route::get("travels/{travel:slug}/tours", [TourController::class, "index"])->middleware('forceJson');
Hello Povilas! You have done an awesome mentor course! It is novel and unusual. Well done!!! $query->when($priceFrom, function (Builder $query, $filters) { $query->where('price', '>=', $priceFrom * 100); }); The when() method have a problem. In case of $priceFrom = 0 the callable function wont activate. It is a specific feature or even a bug of the method. To fix it I have used an additional method: private function checkVal(array $filters, string $key): mixed { return isset($filters[$key]) ? $filters : false; } public function scopeFilter(Builder $query, array $filters) { $query->when($this->checkVal($filters, 'priceFrom'), function (Builder $query, $filters) { $query->where('price', '>=', $filters['priceFrom'] * 100); }); } Do you have another ideas how to fix it?
why we don't use solid. This code is so bad. You are a good teacher, but why not show an example of a good implementation with splitting into services and repositories
Thank you for the amazing tutorial.
The test case "test_tours_list_filters_by_price_correctly" fails because we multiply the price values in TourController for filtering. So, we need to either increase the 'price' we pass to the factory by multiplying it by 100, or multiply the 'priceFrom' value from the query parameter by 100 before passing it.
good video, as always, only have one question, what would you do to make the code in this controller smaller? cheers from Brasil
There are a few packages for filtering and ordering, maybe would use them.
Or move the logic to some TourService.
But I don't see a big need in this specific case.
Thank you very much!
nice way, i prefer if if if :)
As always, a personal preference :)
is there any other configuration need to be done in order to use form request to validate. my form request class does not seem to do the job. it redirect to home page on browser and in postman it returns a 200 status with no errors
Have you specified header "Accept: application/json"? Apart from that, it's hard to debug the error without seeing the code.
That happens because the default response of FormRequest is a Redirect Back with errors, the errors are on the session and you can get them on the frontend via the session helper. If you want to return a JSON response error, you must set the Request Header "Accept" to "application/json". With that you tell Laravel that you want a Json response. If you want every response to be a json, like in a API, you must create a middleware to automaticly set this header.
Thank you both for your responses. setting the header got it working.
@@ArthurHenriqueta where do you set this request header? thanks.
Thx
I will prefer to use pipeline for filtering.
Also an option, yes.
Hello Povilas!
I came across an issue when i was following the lesson.
After putting validation in the code, i tried it in Postman. When provided correct query params - the result was as expected. But when i put sortOrder=random, i saw a default Laravel welcome page as a result. Same thing in browser.
I've got puzzled for a few minutes. Why i don't see validation error!?
When inspecting developer tools in the browser, i noticed that when you send request with wrong values - it responses with 302 (redirect) and then loads welcome page.
Then i inspected Postman headers. Accept is set as '*/*' by default. I overrode it with 'application/json' and then i begun to see the correct error. Good.
But.. why in the browser it does 302 and heads to welcome page instead? How can i get the expected validation error in json there !?
The test also failed:
FAILED Tests\Feature\ToursListTest > tour list returns validation errors
Expected response status code [422] but received 302.
The following errors occurred during the last request:
The date from field must be a valid date.
At this point i don't know what to do.
The test should be launched as postJson() and not just post().
And in postman, for APIs, always use Accept: application/json as header.
@@LaravelDaily aah... yeah, you are right! I simply didn't notice you used `$response->getJson()` in the last one. All the rest were `$response->get()`. Thank you! My bad :)
I faced your exact issue with postman. Solution. Created a middleware with this line $request->headers->set('Accept', 'application/json'); then registered it in $middlewareAliases array in Kernel.php as 'forceJson' => \App\Http\Middleware\ForceJsonResponse::class. Then chained the middleware when making the request this way Route::get("travels/{travel:slug}/tours", [TourController::class, "index"])->middleware('forceJson');
Hello Povilas!
You have done an awesome mentor course! It is novel and unusual. Well done!!!
$query->when($priceFrom,
function (Builder $query, $filters) {
$query->where('price', '>=', $priceFrom * 100);
});
The when() method have a problem. In case of $priceFrom = 0 the callable function wont activate. It is a specific feature or even a bug of the method.
To fix it I have used an additional method:
private function checkVal(array $filters, string $key): mixed
{
return isset($filters[$key]) ? $filters : false;
}
public function scopeFilter(Builder $query, array $filters)
{
$query->when($this->checkVal($filters, 'priceFrom'),
function (Builder $query, $filters) {
$query->where('price', '>=', $filters['priceFrom'] * 100);
});
}
Do you have another ideas how to fix it?
But logically thinking, isn't priceFrom=0 the same as "any price from"? I don't think it needs fixing.
why we don't use solid. This code is so bad. You are a good teacher, but why not show an example of a good implementation with splitting into services and repositories
I always watching your Laravel contents and apply it on my project right now in my Intern Project. More contents to come. Thank you so much :D