parent = File::query()->where('id', $this->input('parent_id'))->first(); // continue return true; } protected function prepareForValidation() { // cast to boolean $this->merge([ 'all' => filter_var($this->all, FILTER_VALIDATE_BOOL), ]); } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { if ($this->parent) { // validate return [ 'parent_id' => [ Rule::exists(File::class, 'id') ->where(function (Builder $query) { return $query ->where('is_folder', '1'); // must be a folder } ) ], 'all' => 'bool', // all or not 'Ids.*' => Rule::exists('files', 'id'), // check that files exist in database ]; } else { return [ 'all' => 'bool', // all or not 'Ids.*' => Rule::exists('files', 'id'), // check that file exists in database ]; } } }