[04/10] Laravel Travel API: Tours Filtering and Ordering

preview_player
Показать описание
We continue our course about Laravel Travel API. Today we're adding the filtering and sorting to the tours API endpoint, validating parameters and adding tests.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

good video, as always, only have one question, what would you do to make the code in this controller smaller? cheers from Brasil

RenanFxDs
Автор

Thank you for the amazing tutorial.
The test case 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.

pushparajbhatta
Автор

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

binuraernest
Автор

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 !?

НазарПанасюк-уя
Автор

I will prefer to use pipeline for filtering.

RajenTrivedi-sp
Автор

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?

Denis-tkum
Автор

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

andrewc
Автор

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

iamjohndevx