Example of Custom validation rules in Ayenko4all/school-management-system-backend

File app/Rules/CreateClassroomRule.php (link to Github)
use 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.';
    }
}
File app/Http/Requests/CreateClassroomRequest.php (link to Github)
use 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'
        ];
    }
}

Additional resources on custom validation rules: