-
app/Rules/CreateClassroomRule.php
Open in GitHubuse App\Models\Classroom; use App\Models\Subject; use Illuminate\Contracts\Validation\Rule; class CreateClassroomRule implements Rule { private $name; private $session; public function __construct($name, $session) { $this->name = $name; $this->session = $session; } public function passes($attribute, $value) { return Classroom::where('name', $this->name) ->where('session_id', $this->session)->doesntExist(); } public function message() { return 'The :attribute already exists for the given session.'; } }
-
app/Http/Requests/CreateClassroomRequest.php
Open in GitHubuse App\Rules\CreateClassroomRule; use Illuminate\Foundation\Http\FormRequest; class CreateClassroomRequest extends FormRequest { public function authorize() { return true; } public function rules() { return [ 'name' => ['required', new CreateClassroomRule($this->input('name'), $this->input('session'))], 'session' => ['required', 'exists:sessions,id'], ]; } public function messages() { return [ '*.required' => 'The :attribute field is required', '*.exists' => 'The selected :attribute does not exist' ]; } }