1

I'm building a Laravel app, and I need to use an URL that looks like that :

/api/ads?page=Actuel&formatsQuery[]=side&formatsQuery[]=leaderboard&deviceQuery=mobile

I have 3 parameters (page, formatsQuery (as an array), and deviceQuery).

Do you now how to hold his in routing and controller in order to have the correct value inside the controller's fonction?

I tried this : routes/api.php

//request to get ads for given parameters
Route::get('/ads', [MediaController::class, 'findAds']);

and this (MediaController.php) :

public function findAds($page, $formatsQuery, $deviceQuery) {
      echo $page;
      if(sizeof($formatsQuery) <= 0 || sizeof($formatsQuery) > 3){
        return $this->unvalidParametersError();
      }
      //transform format to position depending on deviceQuery
      $position = [];
      $res = [];
      foreach ($formatsQuery as $format) {
        $res =  Media::where('position', $format)->inRandomOrder()->first()->union($res);
      }
      echo $res;
      return $res;
    }

then I test it with this :

public function test_findAds()
    {
      $ads = Ad::factory()
            ->has(Media::factory()->count(3), 'medias')
            ->count(3)->create();
      $response = $this->get('/api/ads?page=Actuel&formatsQuery[]=side&formatsQuery[]=leaderboard&deviceQuery=mobile');

      $response->assertStatus(200);
    }

2
  • 1
    Read the documentation, it is pretty explanatory... In your case, you are using a get to send that URL (you should be doing so), so you simply do $request->input('page'), and it should return Actuel. And do that with all your input. For arrays, don't write [] after the name... Commented Aug 30, 2021 at 9:13
  • Have you tried something? add your code. You can read the query using the Request object in your controller. Commented Aug 30, 2021 at 9:13

1 Answer 1

1

You are using a GET request to fetch your data. GET request is a type of request that you send parameters in URL using ? after URL and separating parameters with &. You can find out more about HTTP methods here.

In laravel using request parameters is so simple. First of all you need to add Request $request to your method prototype like this:

use Illuminate\Http\Request;

public function findAds(Request $request)

Then you can simply use $request->parameter to get the values. So you need to change your code like this:

public function findAds(Request $request){
    $page = $request->page;
    $formatsQuery = $request->formatsQuery;
    $deviceQuery = $request->deviceQuery;

    // Your code
}

And as @matiaslauriti mentioned in the comments you don't need to put [] after formatsQuery[] to send an array in GET request. Using the same key more than one time automatically makes an array for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.