I want some opinions about my code that implements store and update for a Laravel 8 form request. The make function is for storing the data and the update function is for updating the data.
<?php
namespace App\Http\Requests;
use App\Models\Admin;
use Illuminate\Foundation\Http\FormRequest;
class AdminRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
if (request()->isMethod('post')) {
$rule = 'required';
}
if (request()->isMethod('put')) {
$rule = 'sometimes';
}
return [
'name' => ['required'],
'email' => [$rule, 'email', Rule::unique('users')->ignore(optional($this)->admin)],
'password' => [$rule, Rules\Password::defaults(), 'confirmed']
];
}
public function make()
{
$data = $this->validated();
$data['email_verified_at'] = \now();
return Admin::create($data);
}
public function update($admin)
{
$data = $this->validated();
$data['email_verified_at'] = \now();
return $admin->update($data);
}
}