php 带有“if”条件的 Laravel 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21938952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:33:56  来源:igfitidea点击:

Laravel query with "if" conditions?

phpsqllaravel

提问by Jeton Tha?i

I'm trying to make an advanced search form with Laravel 4, and this is the query:

我正在尝试使用 Laravel 4 制作高级搜索表单,这是查询:

$result = DB::table('users_ads')
        ->join('ads', 'users_ads.ad_id', '=', 'ads.id')         
        ->orderBy($column, $method)
        ->where('status', TRUE)
        ->where(function($query) use ($input)
        {
            $query->where('short_description', $input['search'])
                    ->where('category', $input['category'])
                    ->where('product', $input['product']);

        })
        ->join('users', 'users_ads.user_id', '=', 'users.id')
        ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')
        ->get();

    return $result;

The problem is that the user might not use all the input fields. So i want to include some if conditions in this part:

问题是用户可能不会使用所有输入字段。所以我想在这部分包含一些 if 条件:

$query->where('short_description', $input['search'])
                    ->where('category', $input['category'])
                    ->where('product', $input['product']);

.. so if the input is empty, to remove the "where" condition.

..所以如果输入为空,则删除“where”条件。

回答by Joe

You could wrap each where in an if statement.

您可以将每个 where 包装在 if 语句中。

$query = DB::table('user_ads')
            ->join('ads', 'users_ads.ad_id', '=', 'ads.id')
            ->orderBy($column, $method);

if ($input['search']) {
    $query->where('short_description', $input['search']);
}

if ($input['category']) {
    $query->where('category', $input['category']);
}

$query->join('users', 'users_ads.user_id', '=', 'users.id')
    ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')

$result= $query->get();

return $result;

Something along those lines would work I believe.

我相信沿着这些路线的东西会起作用。

回答by Joseph Silber

$filters = [
    'short_description' => 'search',
    'category' => 'category',
    'product' => 'product',
];

.....

->where(function($query) use ($input, $filters)
{
    foreach ( $filters as $column => $key )
    {
        $value = array_get($input, $key);

        if ( ! is_null($value)) $query->where($column, $value);
    }
});

Newer version of Laravel have a whenmethod that makes this much easier:

较新版本的 Laravel 有一种when方法可以让这变得更容易:

->where(function ($query) use ($input, $filters) {
    foreach ($filters as $column => $key) {
        $query->when(array_get($input, $key), function ($query, $value) use ($column) {
            $query->where($column, $value);
        });
    }
});