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

ErikRobles's avatar

Target class [currentTenant] does not exist Multi-tenancy

I am learning about Mulit-Tenancy in Laravel and am running into an issue and don't know exactly where to look to isolate the issue. I am following a tutorial https://www.youtube.com/watch?v=uvnvYzQ2fw0&t=187s and at 17 minutes into the video, I need to show the current Tenant by adding the {{app('currentTenant')}} line in the welcome.blade document. This trhows the error: Target class [currentTenant] does not exist I followed the documentation of the package I am using which can be found at https://spatie.be/docs/laravel-multitenancy/v1/basic-usage/automatically-determining-the-current-tenant where I add the 'tenant_finder' code to my multitenancy.php file located in the config folder. I am at a loss as to which code I need to modify to make this work. Any help would be greatly appreciated. I would also like to know if I can show any code and which peice to make helping me a bit easier but like I said, I am not sure what the offending or missing code is just yet. Thank you. In RouteServiceProvider.php, I have:

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */

    protected $namespace = 'App\Http\Controllers';

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

In config/multitenancy.php, I have:

	<?php

use Spatie\Multitenancy\Models\Tenant;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Broadcasting\BroadcastEvent;
use Spatie\Multitenancy\Actions\MigrateTenantAction;
use Illuminate\Notifications\SendQueuedNotifications;
// use Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask;
use Spatie\Multitenancy\Actions\MakeTenantCurrentAction;
// use Spatie\Multitenancy\TenantFinder\DomainTenantFinder;
use Spatie\Multitenancy\Actions\ForgetCurrentTenantAction;
use Spatie\Multitenancy\Actions\MakeQueueTenantAwareAction;

return [
    /*
     * This class is responsible for determining which tenant should be current
     * for the given request.
     *
     * This class should extend `Spatie\Multitenancy\TenantFinder\TenantFinder`
     *
     */
    'tenant_finder' => Spatie\Multitenancy\TenantFinder\DomainTenantFinder::class,

    /*
     * These fields are used by tenant:artisan command to match one or more tenant
     */
    'tenant_artisan_search_fields' => [
        'id',
    ],

    /*
     * These tasks will be performed when switching tenants.
     *
     * A valid task is any class that implements Spatie\Multitenancy\Tasks\SwitchTenantTask
     */
    'switch_tenant_tasks' => [
        Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask::class,
    ],

    /*
     * This class is the model used for storing configuration on tenants.
     *
     * It must be or extend `Spatie\Multitenancy\Models\Tenant::class`
     */
    'tenant_model' => Tenant::class,

    /*
     * If there is a current tenant when dispatching a job, the id of the current tenant
     * will be automatically set on the job. When the job is executed, the set
     * tenant on the job will be made current.
     */
    'queues_are_tenant_aware_by_default' => true,

    /*
     * The connection name to reach the tenant database.
     *
     * Set to `null` to use the default connection.
     */
    'tenant_database_connection_name' => null,

    /*
     * The connection name to reach the landlord database
     */
    'landlord_database_connection_name' => 'landlord',

    /*
     * This key will be used to bind the current tenant in the container.
     */
    'current_tenant_container_key' => 'currentTenant',

    /*
     * You can customize some of the behavior of this package by using our own custom action.
     * Your custom action should always extend the default one.
     */
    'actions' => [
        'make_tenant_current_action' => MakeTenantCurrentAction::class,
        'forget_current_tenant_action' => ForgetCurrentTenantAction::class,
        'make_queue_tenant_aware_action' => MakeQueueTenantAwareAction::class,
        'migrate_tenant' => MigrateTenantAction::class,
    ],

    /*
     * You can customize the way in which the package resolves the queuable to a job.
     *
     * For example, using the package laravel-actions (by Loris Leiva), you can
     * resolve JobDecorator to getAction() like so: JobDecorator::class => 'getAction'
     */
    'queueable_to_job' => [
        SendQueuedMailable::class => 'mailable',
        SendQueuedNotifications::class => 'notification',
        CallQueuedListener::class => 'class',
        BroadcastEvent::class => 'event',
    ],
];

In my Welcome.blade.php, I am calling:

  <p style="color:white;">{{ app('currentTenant') }}</p>

In my Kernal.php, I am calling tenant as such:

	 'tenant' => [
            \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
            \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class,
        ],

If I am missing anything, please let me know.

0 likes
2 replies
softmixt's avatar

Check domain column in your tenants table be sure there is no port number added to your record domain value I think should be plain domain name like tenant1.domain.local not tenant1.domain.local:123 .

This was the issue on my case having same error.

aboozar's avatar

The solution to realize if Tenant has initiated or not is this:

    $containerKey = config('multitenancy.current_tenant_container_key');
    if (app()->has($containerKey)) {
        // you are in Tenant mode (Tenant has found)
    } else {
        // you are in Landlord mode (No Tenant has been found)
    }
1 like

Please or to participate in this conversation.