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

SilvaDreamdeal's avatar

Target class [cache] does not exist Container.php

Hi

I have an app running local without any problem

Started to deploy to forge and got that error

Any idea what could be?

Is there a file that I need to list here?

-> In Container.php line 879:

-> Target class [cache] does not exist.

-> In Container.php line 877:

-> Class "cache" does not exist

The main change is that I'm using tenancy So I added this line to app.php

App\Providers\TenancyServiceProvider::class, // <-- here

Tried to remove just to see if there's some

I found a post here, made what its suggested and still in the same

Class cache does not exist

A - I Deleted vendor and composer.lock still without success.

B - Removed vendor and composer.lock still without success.

C - Deleted vendor folder, run composer install and got error

TenacyServiceProvider.php

<?php

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Jobs;
use Stancl\Tenancy\Listeners;
use Stancl\Tenancy\Middleware;

class TenancyServiceProvider extends ServiceProvider
{
    // By default, no namespace is used to support the callable array syntax.
    public static string $controllerNamespace = '';

    public function events()
    {
        return [
            // Tenant events
            Events\CreatingTenant::class => [],
            Events\TenantCreated::class => [
                JobPipeline::make([
                    Jobs\CreateDatabase::class,
                    Jobs\MigrateDatabase::class,
                    // Jobs\SeedDatabase::class,

                    // Your own jobs to prepare the tenant.
                    // Provision API keys, create S3 buckets, anything you want!

                ])->send(function (Events\TenantCreated $event) {
                    return $event->tenant;
                })->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
            ],
            Events\SavingTenant::class => [],
            Events\TenantSaved::class => [],
            Events\UpdatingTenant::class => [],
            Events\TenantUpdated::class => [],
            Events\DeletingTenant::class => [],
            Events\TenantDeleted::class => [
                JobPipeline::make([
                    Jobs\DeleteDatabase::class,
                ])->send(function (Events\TenantDeleted $event) {
                    return $event->tenant;
                })->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
            ],

            // Domain events
            Events\CreatingDomain::class => [],
            Events\DomainCreated::class => [],
            Events\SavingDomain::class => [],
            Events\DomainSaved::class => [],
            Events\UpdatingDomain::class => [],
            Events\DomainUpdated::class => [],
            Events\DeletingDomain::class => [],
            Events\DomainDeleted::class => [],

            // Database events
            Events\DatabaseCreated::class => [],
            Events\DatabaseMigrated::class => [],
            Events\DatabaseSeeded::class => [],
            Events\DatabaseRolledBack::class => [],
            Events\DatabaseDeleted::class => [],

            // Tenancy events
            Events\InitializingTenancy::class => [],
            Events\TenancyInitialized::class => [
                Listeners\BootstrapTenancy::class,
            ],

            Events\EndingTenancy::class => [],
            Events\TenancyEnded::class => [
                Listeners\RevertToCentralContext::class,
            ],

            Events\BootstrappingTenancy::class => [],
            Events\TenancyBootstrapped::class => [],
            Events\RevertingToCentralContext::class => [],
            Events\RevertedToCentralContext::class => [],

            // Resource syncing
            Events\SyncedResourceSaved::class => [
                Listeners\UpdateSyncedResource::class,
            ],

            // Fired only when a synced resource is changed in a different DB than the origin DB (to avoid infinite loops)
            Events\SyncedResourceChangedInForeignDatabase::class => [],
        ];
    }

    public function register()
    {
        //
    }

    public function boot()
    {
        $this->bootEvents();
        $this->mapRoutes();

        $this->makeTenancyMiddlewareHighestPriority();
    }

    protected function bootEvents()
    {
        foreach ($this->events() as $event => $listeners) {
            foreach ($listeners as $listener) {
                if ($listener instanceof JobPipeline) {
                    $listener = $listener->toListener();
                }

                Event::listen($event, $listener);
            }
        }
    }

    protected function mapRoutes()
    {
        if (file_exists(base_path('routes/tenant.php'))) {
            Route::namespace(static::$controllerNamespace)
                ->group(base_path('routes/tenant.php'));
        }
    }

    protected function makeTenancyMiddlewareHighestPriority()
    {
        $tenancyMiddleware = [
            // Even higher priority than the initialization middleware
            Middleware\PreventAccessFromCentralDomains::class,

            Middleware\InitializeTenancyByDomain::class,
            Middleware\InitializeTenancyBySubdomain::class,
            Middleware\InitializeTenancyByDomainOrSubdomain::class,
            Middleware\InitializeTenancyByPath::class,
            Middleware\InitializeTenancyByRequestData::class,
        ];

        foreach (array_reverse($tenancyMiddleware) as $middleware) {
            $this->app[\Illuminate\Contracts\Http\Kernel::class]->prependToMiddlewarePriority($middleware);
        }
    }
}

App.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\HorizonServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\FortifyServiceProvider::class,
        Maatwebsite\Excel\ExcelServiceProvider::class,
        Barryvdh\DomPDF\ServiceProvider::class,
        App\Providers\TenancyServiceProvider::class, // <-- here
    ],
0 likes
13 replies
anuzpandey's avatar

Comment out whatever you have on your TenencyServiceprovider class within register or boot method and run composer install. If its a success. Then Uncomment the TenencyServiceprovider class. It would be more helpful if you'd show the content of TenencyServiceprovider class, if it can be shown here.

2 likes
SilvaDreamdeal's avatar

@anuzpandey yes, still the same problem

I tried to move the TenancyServiceProvider::class to first position of app providers list, still getting troubles

Its strange because if deploy in the Forge interface it gives that error of the cache

But if I use putty and run inside the server directly the composer install and run the migrations, everything works

anuzpandey's avatar

@SilvaDreamdeal Then I guess the issue here is with the Forge Deploy script. Would you mind sharing what your Forge deploy script looks like?

SilvaDreamdeal's avatar

@anuzpandey after hour messages, I tried again to run things manually. Everything is working by doing it manually With forge is not working. I even tried to use WinSCP to send my project directly to server and it worked fine I ran composer install and no problems

If I send the project to gitlab and then forge gets the project, it gets error

My deploy script:

cd /home/forge/backend
git pull origin $FORGE_SITE_BRANCH

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

if [ -f artisan ]; then
    $FORGE_PHP artisan migrate --force
fi

SilvaDreamdeal's avatar

@anuzpandey I've done everything manually inside server using putty

But still thinking about what's happening when I send the project to gitlab and forge gets the project from the repository I've runned composer install and migrations manually without any problem Now I can do the deploy via Forge. But this first step should be done in forge side, like other projects

anuzpandey's avatar

@SilvaDreamdeal Your deploy script seems fine. I have never used forge so I cannot help you with this. I'm so sorry.

One thing though, please check in the composer.json file and within script section do you have anything related to cache. (I don't think you have, because when you're running it manually its working fine.) May be someone from forge can help you out. They should have some help/support system. You can ask help there.

tykus's avatar

So I added this line to app.php

Where in app.php did you add the TenancyServiceProvider?

SilvaDreamdeal's avatar
SilvaDreamdeal
OP
Best Answer
Level 1

I know this was some time ago

But the answer was that for some motive, my database.php file inside the config folder was set to git ignore and never sent to gitlab

fpresent's avatar

I came across this issue recently, my issue was Spatie packages (which are subdependancies of laravel i think) got updated undercover and broke my project. if someone comes across this issue in this time, check some Spatie package versions (havent checked which exact package yet but one of these is my issue (these package versions fixed the issue for me) [this is on laravel 9]

	"spatie/backtrace": "1.7.1",
    "spatie/enum": "3.13.0",
    "spatie/flare-client-php": "1.10.0",
    "spatie/ignition": "1.14.2",
    "spatie/invade": "1.1.1",
    "spatie/laravel-analytics": "4.1.1",
    "spatie/laravel-package-tools": "1.18.0",
    "spatie/macroable": "2.0.0",
    "spatie/ray": "1.41.4"

Please or to participate in this conversation.