PHP code example of duc_cnzj / model-filter

1. Go to this page and download the library: Download duc_cnzj/model-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duc_cnzj / model-filter example snippets


# User.php

class User extends Model 
{
    use HasFilter;
}

User::filter($request)->get();
or
User::filter(new UserFilter($request))->get();
or
User::filter(['name' => 'duc'])->get();

User::filter($request, ['name'])->get();

User::filter($request, ['name'], 'user')->get();

# 默认是
return array_filter($inputs, function ($item) {
    return !is_null($item);
});

# 自定义的话,请在 boot 方法这样写
Filter::setGetFilterCallback('array_filter'); //callback 第一个参数就是inputs
Filter::setGetFilterCallback(function ($items) {
    return array_filter($items, function ($value, $key) {
        return $key !== 'sb';
    }, ARRAY_FILTER_USE_BOTH);
}); //callback 第一个参数就是inputs