Answer the question
In order to leave comments, you need to log in
Validation of a field that can be either a string or an integer?
if (!is_numeric($request->get('search_text'))) {
$this->validate($request, [
'search_text' => 'required|filled|string|min:1|max:55|',
]);
} else {
$this->validate($request, [
'search_text' => 'required|filled|integer|min:1|max:55|',
]);
}
Answer the question
In order to leave comments, you need to log in
$this->validate($request, [
'type' => 'required|filled|string|min:2|max:15|',
'order' => 'required|filled|string|min:3|max:4|',
'search_text' => 'required|filled|string|min:1|max:55|',
]);
Post::whereRaw("CAST(id AS TEXT) ILIKE '%$search_text%'")->get()
Validate always as a string, and pass the value cast to the required type into the query.
$this->validate($request, [
'search_text' => 'required|filled|string|max:55',
]);
if (preg_match('/^\d+$/', $search)) {
// find_by_id ((int)$search)
} else {
// find_by_name ($search)
}
It is necessary not to edit the validation, but to bring the line to the integer before using it.
The validation rule 'integer'
will pass and 42
and '42'
, and the rule string|min:1|max:55
is a string from 1 to 55 characters long.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question