Company::query() ->withCount(['users', 'warehouses', 'licenseKeys']) ->latest('id') ->paginate(20), ]); } /** * Show the create company screen. */ public function create() { return view('company::create', [ 'company' => new Company([ 'status' => 'active', 'timezone' => 'Europe/London', ]), ]); } /** * Store a company tenant. */ public function store(Request $request) { $company = Company::create($this->validated($request)); return redirect() ->route('platform.companies.show', $company) ->with('status', 'Company created.'); } /** * Display a company workbench. */ public function show(Company $company) { $company->loadCount(['users', 'warehouses', 'licenseKeys', 'payments']); return view('company::show', [ 'company' => $company, 'recentUsers' => $company->users()->latest('id')->limit(5)->get(), 'recentWarehouses' => $company->warehouses()->latest('id')->limit(5)->get(), 'recentLicenseKeys' => $company->licenseKeys()->latest('id')->limit(5)->get(), ]); } /** * Show the edit company screen. */ public function edit(Company $company) { return view('company::edit', [ 'company' => $company, ]); } /** * Update a company tenant. */ public function update(Request $request, Company $company) { $company->update($this->validated($request, $company)); return redirect() ->route('platform.companies.show', $company) ->with('status', 'Company updated.'); } /** * Delete a company tenant. */ public function destroy(Company $company) { abort_if($company->is_system_company, 403, 'The system company cannot be deleted.'); $company->delete(); return redirect() ->route('platform.companies.index') ->with('status', 'Company deleted.'); } private function validated(Request $request, ?Company $company = null): array { $data = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'billing_email' => ['nullable', 'email', 'max:255'], 'status' => ['required', Rule::in(['active', 'suspended', 'inactive'])], 'timezone' => ['nullable', 'string', 'max:64'], ]); $data['slug'] = Str::slug($data['name']); $data['timezone'] = $data['timezone'] ?? ($company?->timezone ?: 'Europe/London'); return $data; } }