Search code examples
laravelunirestrapidapi

How to fetch Rapidapi in laravel Controller?


I am trying to fetch API using unirest in my laravel application. I followed all the steps given in the docs but it's showing me an error. If there is a better alternative for unirest please let me know. Thanks in advance!

Here is my controller,

public function store(Request $request)
    {

        Love::create(
            request()->validate([
                'name_1' => ['required','max:255'],
                'name_2' => ['required','max:255'],
            ],
            [
                'name_1.required' => 'You have to Enter Your name',
                'name_2.required' => 'You have to Enter Your Crush name'
            ]));

            $headers = array('Accept' => 'application/json');

            $response = Unirest\Request::post("API_URL",
              array(
                "X-RapidAPI-Key" => "API_KEY"
              )
            );
                dd($response);

            return view("result");
   }

Error

Class 'App\Http\Controllers\Unirest\Request' not found

enter image description here


Solution

  • You need to import the Unirest\Request class.

    <?php
    
    namespace Your\Namespace;
    
    use Unirest\Request;
    
    class YourClass{
    ...
    

    If you don't import it, it will by default look for the class in the current namespace (in your case, App\Http\Controllers).