Laravel数组whereIn()

时间:2017-01-15 17:30:15

标签: php laravel-5.3

我有一个表单来过滤项目:

filter

我在Laravel 5.3中寻找类似的东西:

// some variables get from request()->input('...')
$mode = ['A'];
$type = ['a', 'b'];
$group = [0, 1];

// desirable query
$results = Item::whereIn([
    ['mode_id', $mode],
    ['type_id', $type],
    ['group_id', $group]
])->paginate(10);

我可以这样做

$results = Item::whereIn('mode_id', $mode)
               ->whereIn('type_id', $type)
               ->whereIn('group_id', $group)
               ->paginate(10);

但它不是一种动态的方式。例如,如果用户在模式下不选择任何内容,则查询将返回一个空数组。

2 个答案:

答案 0 :(得分:2)

我们可以使用conditional clauses

$results = Item::
    when(!empty($mode), function ($query) use ($mode) {
        return $query->where('mode_id', $mode);
    })
    ->when(!empty($type), function ($query) use ($type) {
        return $query->where('type_id', $type);
    })
    ->when(!empty($group), function ($query) use ($group) {
        return $query->where('group_id', $group);
    })
    ->paginate(10);

答案 1 :(得分:1)

你可以这样做:

java.lang.SecurityException: JCE cannot authenticate the provider BC
        at javax.crypto.Cipher.getInstance(Cipher.java:657)
        at javax.crypto.Cipher.getInstance(Cipher.java:596)
        at myPackage.Cryption.decrypt(Cryption.java:118)
        at myPackage.Cryption.decryptToObject(Cryption.java:107)
        at myPackage.main.lambda$0(main.java:122)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.util.jar.JarException: file:/C:/[...]/MyJar.jar has unsigned entries - myPackage/Cryption.class
        at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:500)
        at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:361)
        at javax.crypto.JarVerifier.verify(JarVerifier.java:289)
        at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:159)
        at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:185)
        at javax.crypto.Cipher.getInstance(Cipher.java:653)
        ... 5 more

或者在传递之前将whereIn关联数组构建为空的位置。