diff --git a/crm-panel/app/Http/Controllers/CompanyController.php b/crm-panel/app/Http/Controllers/CompanyController.php index 9d18602..8cb448f 100644 --- a/crm-panel/app/Http/Controllers/CompanyController.php +++ b/crm-panel/app/Http/Controllers/CompanyController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Models\Company; use Illuminate\Http\Request; +use App\Http\Requests\CompanyRequest; class CompanyController extends Controller { @@ -18,22 +19,21 @@ class CompanyController extends Controller return view('companies.create'); } - public function store(Request $request) + public function store(CompanyRequest $request) { - $request->validate([ - 'name' => 'required', - 'email' => 'nullable|email', - 'logo' => 'nullable|image|dimensions:min_width=100,min_height=100', - 'website' => 'nullable|url', - ]); - + // Store the validated data from the form + $validatedData = $request->validated(); + + // Handle the logo upload if it exists if ($request->hasFile('logo')) { - $filename = $request->file('logo')->store('logos', 'public'); - $request->merge(['logo' => $path]); + $logoPath = $request->file('logo')->store('logos', 'public'); + $validatedData['logo'] = $logoPath; } - - Company::create($request->all()); - + + // 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.'); } @@ -47,32 +47,24 @@ class CompanyController extends Controller return view('companies.edit', compact('company')); } - public function update(Request $request, Company $company) -{ - $request->validate([ - 'name' => 'required', - 'email' => 'nullable|email', - 'logo' => 'nullable|image|dimensions:min_width=100,min_height=100', - 'website' => 'nullable|url', - ]); - - // Handle logo update - if ($request->hasFile('logo')) { - // Store the file in 'public/logos' and get the path - $path = $request->file('logo')->store('logos', 'public'); - $company->logo = $path; - } - - // Update other fields - $company->name = $request->name; - $company->email = $request->email; - $company->website = $request->website; + 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.'); + } - $company->save(); // Save the changes - - return redirect()->route('companies.index')->with('success', 'Company updated successfully.'); -} - public function destroy(Company $company) { $company->delete(); diff --git a/crm-panel/app/Http/Controllers/EmployeeController.php b/crm-panel/app/Http/Controllers/EmployeeController.php index 2839f1f..22f0cf7 100644 --- a/crm-panel/app/Http/Controllers/EmployeeController.php +++ b/crm-panel/app/Http/Controllers/EmployeeController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Employee; use App\Models\Company; use Illuminate\Http\Request; +use App\Http\Requests\EmployeeRequest; class EmployeeController extends Controller { @@ -20,19 +21,18 @@ class EmployeeController extends Controller return view('employees.create', compact('companies')); } - public function store(Request $request) + public function store(EmployeeRequest $request) { - $request->validate([ - 'first_name' => 'required', - 'last_name' => 'required', - 'company_id' => 'required|exists:companies,id', - 'email' => 'nullable|email', - 'phone' => 'nullable|string', - ]); - - Employee::create($request->all()); - - return redirect()->route('employees.index'); + $employee = new Employee(); + $employee->first_name = $request->first_name; + $employee->last_name = $request->last_name; + $employee->company_id = $request->company_id; + $employee->email = $request->email; + $employee->phone = $request->phone; + + $employee->save(); + + return redirect()->route('employees.index')->with('success', 'Employee created successfully!'); } public function show(Employee $employee) diff --git a/crm-panel/app/Http/Requests/CompanyRequest.php b/crm-panel/app/Http/Requests/CompanyRequest.php new file mode 100644 index 0000000..c863c66 --- /dev/null +++ b/crm-panel/app/Http/Requests/CompanyRequest.php @@ -0,0 +1,31 @@ +|string> + */ + public function rules() + { + return [ + 'name' => 'required|string|max:255', // Name is required + 'email' => 'nullable|email|max:255', // Email is optional but must be a valid email if provided + 'logo' => 'nullable|image|dimensions:min_width=100,min_height=100', // Logo must be an image and at least 100x100 pixels + 'website' => 'nullable|url|max:255', // Website is optional but must be a valid URL if provided + ]; + } +} diff --git a/crm-panel/app/Http/Requests/EmployeeRequest.php b/crm-panel/app/Http/Requests/EmployeeRequest.php new file mode 100644 index 0000000..3cb4f32 --- /dev/null +++ b/crm-panel/app/Http/Requests/EmployeeRequest.php @@ -0,0 +1,32 @@ +|string> + */ + public function rules() + { + return [ + 'first_name' => 'required|string|max:255', // First name is required and a string + 'last_name' => 'required|string|max:255', // Last name is required and a string + 'company_id' => 'required|exists:companies,id', // Company should be valid + 'email' => 'nullable|email|max:255', // Email is optional but must be a valid email if provided + 'phone' => 'nullable|string|max:20', // Phone is optional and can be a string up to 20 characters + ]; + } +}