'How to handle if does not have a "current" tenant?

I'm using Spatie laravel-multitenancy. laravel v8

I'm not defined domain one.localhost and now I'm getting an error. so how to handle the error. error is

Spatie\Multitenancy\Exceptions\NoCurrentTenant The request expected a current tenant but none was set. http://one.localhost:8000/

web.php

Route::domain('{tenant}.localhost')->middleware('tenant')->group(function(){
Route::get('/', function ($tenant) {
    return $tenant;
    // return view('welcome');
});

});

Route::domain('localhost')->group(function(){
Route::get('/', function () {
    return view('welcome');
});

});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');})->name('dashboard');


Solution 1:[1]

Hi I am facing the same problem and after Debugging I found that I missed domain in tenants table enter image description here
please add "one.localhost" in tenants table and everthing will be fine

if you don't have tenants table please run

php artisan vendor:publish --provider="Spatie\Multitenancy\MultitenancyServiceProvider" --tag="multitenancy-migrations"
php artisan migrate --path=database/migrations/landlord

Actually this package also provide Exception Handling if tenant did not exist. You can study more in these links

https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/automatically-determining-the-current-tenant
https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/ensuring-a-current-tenant-has-been-set

Solution 2:[2]

Hi i get same error and i solve it by add the handle exception for NoCurrentTenant got to Handler.php and try this register function:

 $this->reportable(function (NoCurrentTenant  $e) {
        abort(404);
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raj Omer Mustafa
Solution 2 Mansour Mehidi