request classes

This commit is contained in:
Aanol Basnet 2025-05-08 15:47:58 +05:45
parent ea35db2164
commit 50c804d445
4 changed files with 105 additions and 50 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Company; use App\Models\Company;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Requests\CompanyRequest;
class CompanyController extends Controller class CompanyController extends Controller
{ {
@ -18,22 +19,21 @@ class CompanyController extends Controller
return view('companies.create'); return view('companies.create');
} }
public function store(Request $request) public function store(CompanyRequest $request)
{ {
$request->validate([ // Store the validated data from the form
'name' => 'required', $validatedData = $request->validated();
'email' => 'nullable|email',
'logo' => 'nullable|image|dimensions:min_width=100,min_height=100',
'website' => 'nullable|url',
]);
// Handle the logo upload if it exists
if ($request->hasFile('logo')) { if ($request->hasFile('logo')) {
$filename = $request->file('logo')->store('logos', 'public'); $logoPath = $request->file('logo')->store('logos', 'public');
$request->merge(['logo' => $path]); $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.'); return redirect()->route('companies.index')->with('success', 'Company created successfully.');
} }
@ -47,32 +47,24 @@ class CompanyController extends Controller
return view('companies.edit', compact('company')); return view('companies.edit', compact('company'));
} }
public function update(Request $request, Company $company) public function update(CompanyRequest $request, Company $company)
{ {
$request->validate([ // Store the validated data from the form
'name' => 'required', $validatedData = $request->validated();
'email' => 'nullable|email',
'logo' => 'nullable|image|dimensions:min_width=100,min_height=100',
'website' => 'nullable|url',
]);
// Handle logo update // Handle the logo upload if it exists
if ($request->hasFile('logo')) { if ($request->hasFile('logo')) {
// Store the file in 'public/logos' and get the path $logoPath = $request->file('logo')->store('logos', 'public');
$path = $request->file('logo')->store('logos', 'public'); $validatedData['logo'] = $logoPath;
$company->logo = $path; }
// 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.');
} }
// Update other fields
$company->name = $request->name;
$company->email = $request->email;
$company->website = $request->website;
$company->save(); // Save the changes
return redirect()->route('companies.index')->with('success', 'Company updated successfully.');
}
public function destroy(Company $company) public function destroy(Company $company)
{ {
$company->delete(); $company->delete();

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Models\Employee; use App\Models\Employee;
use App\Models\Company; use App\Models\Company;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Requests\EmployeeRequest;
class EmployeeController extends Controller class EmployeeController extends Controller
{ {
@ -20,19 +21,18 @@ class EmployeeController extends Controller
return view('employees.create', compact('companies')); return view('employees.create', compact('companies'));
} }
public function store(Request $request) public function store(EmployeeRequest $request)
{ {
$request->validate([ $employee = new Employee();
'first_name' => 'required', $employee->first_name = $request->first_name;
'last_name' => 'required', $employee->last_name = $request->last_name;
'company_id' => 'required|exists:companies,id', $employee->company_id = $request->company_id;
'email' => 'nullable|email', $employee->email = $request->email;
'phone' => 'nullable|string', $employee->phone = $request->phone;
]);
Employee::create($request->all()); $employee->save();
return redirect()->route('employees.index'); return redirect()->route('employees.index')->with('success', 'Employee created successfully!');
} }
public function show(Employee $employee) public function show(Employee $employee)

View 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
];
}
}

View 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
];
}
}