在Laravel上有另一个Where Filter

时间:2017-12-06 21:44:04

标签: php laravel laravel-5 laravel-4

我正在尝试进行搜索,只想返回状态为1,4的用户。虽然当我尝试使用它时,它会抓住它们并忽略我最后的位置。

$officiants = Officiant::where('status',1)
                        ->where('email', 'like',  '%'.$item.'%')
                        ->orWhere('lname', 'like', '%'.$item.'%')
                        ->orWhere('lname', 'like', '%'.$item.'%')
                        ->whereIn('status', [1,4])
                        ->get();

我也试过

$officiants = Officiant::where('status',1)
                        ->where('email', 'like',  '%'.$item.'%')
                        ->orWhere('lname', 'like', '%'.$item.'%')
                        ->orWhere('lname', 'like', '%'.$item.'%')
                        ->where('status', 1)
                        ->where('status', 4)
                        ->get();

3 个答案:

答案 0 :(得分:1)

问题是你需要了解在Laravel中链接和/或链接的工作方式。

当您说$query->where(..a..)->orWhere(..b..)->where(..c..)->orWhere(..d..)时,它会评估为:(a || (b && c) || d)。您可能打算((a || b) && (c || d))或有意((a && c) || b || d)。这就是为什么当你需要高级where子句时,使用parameter grouping

答案 1 :(得分:1)

你需要这样的东西:

$officiants = Officiant::where('status',1)
    ->where(function($query) use($item){
        $query->where('email', 'like',  '%'.$item.'%')
            ->orWhere('lname', 'like', '%'.$item.'%')
            ->orWhere('lname', 'like', '%'.$item.'%')
    })
        ->whereIn('status', [1,4])
        ->get();

注意:这未经过测试

答案 2 :(得分:0)

我刚想通了

public static function getCustomSignedCookies()
{
    $domain = '.' . explode('.', apache_request_headers()['Host'], 2)[1];
    $dt = new DateTime();
    $dt->add(new DateInterval('P1Y')); // 1 year
    $expires = $dt->getTimestamp();
    $url = Config::get('cloudfront_url') . '/*';
    $policy = self::getCustomPolicy($url, $expires);
    $encodedPolicy = self::url_safe_base64_encode($policy);
    $signature = self::getSignature($policy);

    $cookies = [
        [
            'name' => 'CloudFront-Policy'
            , 'value' => $encodedPolicy
            , 'expires' => $expires
            , 'path' => '/'
            , 'domain' => $domain
            , 'secure' => true
            , 'httpOnly' => true
        ],
        [
            'name' => 'CloudFront-Signature'
            , 'value' => $signature
            , 'expires' => $expires
            , 'path' => '/'
            , 'domain' => $domain
            , 'secure' => true
            , 'httpOnly' => true
        ],
        [
            'name' => 'CloudFront-Key-Pair-Id'
            , 'value' => self::$keyPair
            , 'expires' => $expires
            , 'path' => '/'
            , 'domain' => $domain
            , 'secure' => true
            , 'httpOnly' => true
        ]
    ];

    return $cookies;
}

public static function setCloudFrontCookies()
{
    ob_start();

    foreach (self::getCustomSignedCookies() as $cookie)
    {
        setcookie
        (
            $cookie['name']
            , $cookie['value']
            , $cookie['expires']
            , $cookie['path']
            , $cookie['domain']
            , $cookie['secure']
            , $cookie['httpOnly']
        );
    }

    ob_end_flush();
}