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

Поделиться
HTML-код
  • Опубликовано: 2 дек 2024

Комментарии • 23

  • @pushparajbhatta
    @pushparajbhatta 8 месяцев назад

    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.

  • @RenanFxDs01
    @RenanFxDs01 Год назад +1

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

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      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.

  • @АртурЗарипов-ю9п

    Thank you very much!

  • @murat-ersoy
    @murat-ersoy Год назад +1

    nice way, i prefer if if if :)

    • @LaravelDaily
      @LaravelDaily  Год назад +3

      As always, a personal preference :)

  • @binuraernest3988
    @binuraernest3988 Год назад +1

    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

    • @LaravelDaily
      @LaravelDaily  Год назад +2

      Have you specified header "Accept: application/json"? Apart from that, it's hard to debug the error without seeing the code.

    • @ArthurHenriqueta
      @ArthurHenriqueta Год назад +2

      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.

    • @binuraernest3988
      @binuraernest3988 Год назад

      Thank you both for your responses. setting the header got it working.

    • @joshajiniran
      @joshajiniran Год назад

      @@ArthurHenriqueta where do you set this request header? thanks.

  • @narendrasharma1614
    @narendrasharma1614 Год назад

    Thx

  • @RajenTrivedi-s9p
    @RajenTrivedi-s9p Год назад +1

    I will prefer to use pipeline for filtering.

  • @НазарПанасюк-у8я

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

    • @НазарПанасюк-у8я
      @НазарПанасюк-у8я Год назад

      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
      @LaravelDaily  Год назад

      The test should be launched as postJson() and not just post().
      And in postman, for APIs, always use Accept: application/json as header.

    • @НазарПанасюк-у8я
      @НазарПанасюк-у8я Год назад

      @@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 :)

    • @davidmutua6971
      @davidmutua6971 Год назад

      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');

  • @Denis-tk3um
    @Denis-tk3um Год назад

    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?

    • @LaravelDaily
      @LaravelDaily  Год назад

      But logically thinking, isn't priceFrom=0 the same as "any price from"? I don't think it needs fixing.

  • @andrewc3942
    @andrewc3942 Год назад

    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

  • @iamjohndevx
    @iamjohndevx Год назад +1

    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