Description

Validator collection is a subclass of Illuminate\Support\Collection, all are the same except for the following specified differences, which can be found in the Collections documentationopen in new window.

To make it easier to fit the validator,some following changes have been made to the validator collection

  • Update Support for original usage, with new features added
  • Overlay Override original usage, all usage is based on this document
  • New New methods added, not in the original collection, subject to this document

get Update

Returns the item at a given key. If the key does not exist, null is returned Original Documentopen in new window

  • Add the use of . method to get the values in the array
/**
* @method mixed get($key,$default = null)
*/

$data->get('search.keyword');
1
2
3
4
5
  • Add the * method to get the columns in a multi-dimensional array
$data = validate_collect([
    'I7 1700K' => [
        'vendor' => 'Inter'
    ],
    'R7 5800X' => [
        'vendor' => 'AMD'
    ],
    'I9 11900K' => [
        'vendor' => 'Inter'
    ],
    'A10-9700' => [
        'vendor' => 'AMD'
    ]
]);

$data->get('*.vendor');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

has Overlay

Check if an item or items exist in an array using "dot" notation.

/**
* @method bool has($key)
*/

$data->has('search.keyword');
1
2
3
4
5

Multiple Key values can be passed as arrays or multiple parameters

set New

Set an array item to a given value using "dot" notation.

/**
* @method $this set(key, $value)
*/

$this->set('search.keyword','号码')->set("user_id",1);
1
2
3
4
5

whenHas New

Execute the given callback function when the specified field exists, otherwise specify a second callback function (which can be empty)

/**
* @method $this whenHas($key, callable $callback, callable $default = null)
*/

$this->whenHas('id',function($data){
     $id = $data->get('id');
});
1
2
3
4
5
6
7

whenNotHas New

When the specified field does not exist, the given callback function is executed, otherwise a second callback function is specified (which can be empty)

/**
* @method $this whenNotHas($key, callable $callback, callable $default = null)
*/

$this->whenNotHas('id',function($data){
     return 123});
1
2
3
4
5
6
7

Using

  • Use the validate_collect method to quickly get a validator collection object
  • Using the get_validate_data() method to fetch the validated value returns a validator collection object as well
Last Updated: 10/13/2021, 2:39:38 PM