3
votes

In my Laravel app and as a scheduled task, I want to make an Http request in my custom class but I get

Class 'Illuminate\Support\Facades\Http' not found {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Class 'Illuminate\\Support\\Facades\\Http' not found

Here is my custom class

<?php

namespace App\MyModels\GetData;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class Test
{
    public function __invoke()
    {
        Log::info("Cron executed : ");
        $response = Http::get("https://google.com");
        Log::info(print_r($response, true));
    }
}

in Laravel documentation, it says :

To make requests, you may use the get, post, put, patch, and delete methods. First, let's examine how to make a basic GET request:

use Illuminate\Support\Facades\Http;

$response = Http::get('http://test.com');

3
Http is not a Facade that I'm aware of. What documentation are you referencing on its usage?Tim Lewis
@TimLewis here laravel.com/docs/7.x/http-client. I just added its explanation to my questionArtin Artin
Ah, so it is GuzzleHttp; cool. I'm familiar with that. Did you run the composer command? composer require guzzlehttp/guzzleTim Lewis
@TimLewis yes I did!Artin Artin
Ok, next step; run composer dump-autoload, just in case that didn't get updated. Following that, try again, and if that doesn't work, then do php artisan tinker, followed by $http = new Illuminate\Support\Facades\Http();; it should output something. Let me know when you've tried that.Tim Lewis

3 Answers

2
votes

For me, the issue had to do with the fact that (while I thought the project was on version 7 for Laravel) I appeared to be on version 6.x for Laravel. Updated to the newest version solved this issue for me. Link to the GitHub issue that resolved my issue

Although the question is specific about version 7 I still feel this might help some people

1
votes

Based on comments you probably have a non-generic/minimal Laravel version installed.

  1. Back up your current composer.json (from project root)
  2. Open composer.json
  3. Go to require property
  4. Replace all laravel/* properties with default ones:
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0"
  1. Save the file and run composer update && php artisan config:cache

After that all facades should be installed correctly :)


For further reference if you want to create a new Laravel application, don't bother choosing a custom distribution (which may not contain all features as in OP's case). Use the default repository:

composer create-project laravel/laravel MyLaravelProject
0
votes

Try importing the Http in your controller as shown below.

use Illuminate\Support\Facades\Http;

Hope that solves your issue.