request classes
This commit is contained in:
parent
ea35db2164
commit
50c804d445
@ -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();
|
||||
|
@ -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)
|
||||
|
31
crm-panel/app/Http/Requests/CompanyRequest.php
Normal file
31
crm-panel/app/Http/Requests/CompanyRequest.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CompanyRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true; // Change this to true to allow validation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|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
|
||||
];
|
||||
}
|
||||
}
|
32
crm-panel/app/Http/Requests/EmployeeRequest.php
Normal file
32
crm-panel/app/Http/Requests/EmployeeRequest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class EmployeeRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|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
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user