0

I want to pass the id from the controller to route but I'm having trouble with it. I am a newbie at this so I would really appreciate your help!

Controller:

public function fetchVideo(Request $request)
{
    $client = new Client();
    $input = $request->all();

    $headers = [
        'Authorization' => 'Bearer '.$input['token'],
        'Content-type' => 'application/json',
        'Accept' => 'application/json'
    ];

    $params = [
        'id' => $input['id'],
        'fields' => $input['fields']
    ];

    $response = $client->request ('GET', 'https://api.dailymotion.com/video/{id}', [
        'headers' => $headers,
        'query' => $params
    ]);

    return json_decode($response->getBody(), true);
}

Route:

Route::post('/video/{id}', 'App\Http\Controllers\dailymotionController@fetchVideo');
Alison
  • 5
  • 4
  • It usually goes from route to controller, not the other way around. You're requiring the parameter, so you should require it in the function definition. See https://laravel.com/docs/8.x/routing#required-parameters and https://laravel.com/docs/8.x/controllers#basic-controllers – aynber Sep 17 '21 at 17:37
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 25 '21 at 10:48

1 Answers1

1
public function fetchVideo(Request $request, $id) // <- {id} parameter in the route
{
   ...
   $response = $client->request('GET', "https://api.dailymotion.com/video/{$id}", [...])
   ...
}
nikoz84
  • 133
  • 1
  • 8
IGP
  • 14,160
  • 4
  • 26
  • 43