composer.json
{ "require": { "php": "^7.4|^8.0", // "spatie/laravel-multitenancy": "^1.0", },}
{ "require": { "php": "^7.4|^8.0", // "spatie/laravel-multitenancy": "^1.0", },}
use App\Models\Tree;use Illuminate\Auth\Events\Authenticated;use Illuminate\Support\Facades\DB;use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;use Spatie\Multitenancy\Models\Tenant; class TenantAuthenticated{ use UsesLandlordConnection; public function handle(Authenticated $event) { $user = auth()->user(); $company = DB::connection($this->getConnectionName()) ->table('user_company') ->where('user_id', $user->id) ->select('company_id') ->first(); if($company) { $tree = Tree::where('company_id', $company->company_id)->first(); $tenant = Tenant::where('tree_id', $tree->id)->first(); $tenant->makeCurrent(); } }}
use App\Models\Tree;use Illuminate\Auth\Events\Logout;use Illuminate\Support\Facades\DB;use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;use Spatie\Multitenancy\Models\Tenant; class TenantLogout{ use UsesLandlordConnection; public function handle(Logout $event) { $user = auth()->user(); if($user) { $company = DB::connection($this->getConnectionName()) ->table('user_company') ->where('user_id', $user->id) ->select('company_id') ->first(); if($company) { $tree = Tree::where('company_id', $company->company_id)->first(); $tenant = Tenant::where('tree_id', $tree->id)->first(); session()->flush(); $tenant->forgetCurrent(); } } }}