Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j00rd's avatar
Level 2

Can't retrieve the slug from API call.

Hi guys, I'm following the Video Game aggregator series but am struggling now I've moved on to a single page.

  public function show($slug)
    {
        $game = Http::withHeaders([                            /* Use HTTP client with headers of API tokens from .env */
            'Client-ID' => env('IGDB_KEY'),
            'Authorization' => env('IGDB_AUTH'),
        ])  ->withBody(                                                     /* Get the 12 highest rated games with their name and rating */
                'fields name, slug;
                where slug=\"$slug\";                                           
                limit 1;',
                'text/plain'
                )
            ->post('https://api.igdb.com/v4/games')->json();
        
        dump($game);

        return view('show');
    }

Following the video I run in to this error - cause" => "Mismatched input, double check your input. Common cause is sending " instead of "." - I read about single quoted string variables being handled different by PHP so I also tried this method that is working with some previous variable I had however they are hardcoded.

public function show($slug)
    {

        $game = Http::withHeaders([                            /* Use HTTP client with headers of API tokens from .env */
            'Client-ID' => env('IGDB_KEY'),
            'Authorization' => env('IGDB_AUTH'),
        ])  ->withBody(                                                     /* Get the 12 highest rated games with their name and rating */
                'fields name, slug;
                where slug= '.$slug.';                                           
                limit 1;',
                'text/plain'
                )
            ->post('https://api.igdb.com/v4/games')->json();
        
        dump($game);

        return view('show');
    }

This returns me the error - "cause" => "Expecting a STRING as input, surround your input with quotes starting at 'maia' expecting {'{', 'f', '(', '[', 'true', 't', 'false', 'null', 'n'" - So I'm not sure on how retrieve game details where the slug matches the slug that is in the URL and is being passed across.

Any help is greatly appreciated.

0 likes
7 replies
Nakov's avatar

remove the ; from this line where slug= '.$slug.'; because you have limit 1; on the next one, so you are closing it twice. And make sure after the = on slug you don't have a space:

'fields name, slug;
where slug='.$slug.'                               
limit 1;',
j00rd's avatar
Level 2

@Nakov Thanks for the quick response!

I've tried that and updated my code however I'm getting the same result of it expecting a string.

public function show($slug)
    {

        $game = Http::withHeaders([                            /* Use HTTP client with headers of API tokens from .env */
            'Client-ID' => env('IGDB_KEY'),
            'Authorization' => env('IGDB_AUTH'),
        ])  ->withBody(                                                     /* Get the 12 highest rated games with their name and rating */
                'fields name, slug;
                where slug='.$slug.'                                         
                limit 1;',
                'text/plain'
                )
            ->post('https://api.igdb.com/v4/games')->json();
        
        dump($game);

        return view('show');
    }

"cause" => "Expecting a STRING as input, surround your input with quotes starting at 'maia' expecting {'{', 'f', '(', '[', 'true', 't', 'false', 'null', 'n'"

j00rd's avatar
Level 2

@Nakov Someone has replied to me with the answer but I can't see it on the thread only my emails, it was @webrobert so thank you both.

The solution was -

->withBody(                                                    
                'fields name, slug;
                where slug= "'.$slug.'";                                           
                limit 1;',
                'text/plain'
                )

I just had to wrap the '.$slug.' in double quotes.

webrobert's avatar
Level 51

@j00rd oops, ill take the best answer, I deleted it by mistake.

Here it is again...

->withBody(
    'fields name, slug;
     where slug= "'.$slug.'";                                           
     limit 1;',
    'text/plain'
)
1 like
webrobert's avatar

@j00rd, I updated my reply, please mark it as best answer. Glad it helped!

1 like

Please or to participate in this conversation.