CRM-panel/crm-panel/app/Http/Controllers/CompanyController.php
2025-05-08 15:47:58 +05:45

74 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Http\Requests\CompanyRequest;
class CompanyController extends Controller
{
public function index()
{
$companies = Company::paginate(10);
return view('companies.index', compact('companies'));
}
public function create()
{
return view('companies.create');
}
public function store(CompanyRequest $request)
{
// Store the validated data from the form
$validatedData = $request->validated();
// Handle the logo upload if it exists
if ($request->hasFile('logo')) {
$logoPath = $request->file('logo')->store('logos', 'public');
$validatedData['logo'] = $logoPath;
}
// Create the company with the validated data
$company = \App\Models\Company::create($validatedData);
// Redirect or return a response
return redirect()->route('companies.index')->with('success', 'Company created successfully.');
}
public function show(Company $company)
{
return view('companies.show', compact('company'));
}
public function edit(Company $company)
{
return view('companies.edit', compact('company'));
}
public function update(CompanyRequest $request, Company $company)
{
// Store the validated data from the form
$validatedData = $request->validated();
// Handle the logo upload if it exists
if ($request->hasFile('logo')) {
$logoPath = $request->file('logo')->store('logos', 'public');
$validatedData['logo'] = $logoPath;
}
// Update the company with the validated data
$company->update($validatedData);
// Redirect or return a response
return redirect()->route('companies.index')->with('success', 'Company updated successfully.');
}
public function destroy(Company $company)
{
$company->delete();
return redirect()->route('companies.index')->with('success', 'Company deleted successfully.');
}
}