Level 75
Have you viewed right here on laracasts the series on multi-tenant setup.
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Laravel\Jetstream\Http\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'tenant' => [
\App\Http\Middleware\Tenant\Tenant::class,
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
web.php
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::group(['middleware' => ['tenant']], function() {
Route::get('/jobs', [JobController::class, 'index'])->name('jobs.index');
Route::get('/{company}', [DashboardController::class, 'index']);
});
namespace App\Http\Middleware\Tenant;
<?php
namespace App\Http\Middleware\Tenant;
use Closure;
use App\Models\Company;
use App\Tenant\Manager;
use Illuminate\Http\Request;
class Tenant
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
//dd(session()->get('tenant')); //THIS GIVES NULL
$tenant = $this->resolveTenant(
$request->company->id ?: session()->get('tenant')
);
if(!auth()->user()->companies->contains('id', $tenant->id)){
return redirect('/home');
}
$this->registerTenant($tenant);
return $next($request);
}
protected function registerTenant($tenant)
{
app(Manager::class)->setTenant($tenant);
session()->put('tenant', $tenant->id);
//dd(session()->get('tenant')); // THIS WORKS (E.g.- 1)
}
protected function resolveTenant($id)
{
return Company::find($id);
}
}
I am getting this error when i execute this url http://127.0.0.1:8000/jobs
Trying to get property 'id' of non-object
Why can't i get the the jobs of the relevant user session ?
Have you viewed right here on laracasts the series on multi-tenant setup.
Please or to participate in this conversation.