validate([ 'name' => 'required|max:255', 'url' => 'required|unique:tenants|regex:/^[a-z0-9]{8,30}$/i' ]); $tenant = new Tenant(); $tenant->name = $validated["name"]; $tenant->url = $validated["url"]; $tenant->template = "default"; $tenant->owner = Auth::user()->id; $tenant->saveOrFail(); $this->switchTenantById($tenant->id); return redirect("/d"); } public function switchTenant($url, Request $request) { $tenant = Tenant::getByUrl($url); if($tenant->owner == Auth::id()) { $this->switchTenantById($tenant->id); return Redirect::back(); } else { return Redirect::back()->with(["msg" => "No Access to given Tenant"]); } } private function switchTenantById($id) { session(["current_tenant_id" => $id]); } }