26

我可以在 Code Igniter 中做到这一点:

$this->db->select();
$this->from->('node');
if ($published == true)
{
    $this->db->where('published', 'true');
}
if (isset($year))
{
    $this->db->where('year >', $year);
}
$this->db->get();

如何翻译这段代码以便它在 Laravel 中工作?

4

9 回答 9

53

在 Fluent 中,您可以:

$query = DB::table('node');

if ($published == true)
    $query->where('published', '=', 1);

if (isset($year))
    $query->where('year', '>', $year);

$result = $query->get();
于 2013-01-06T06:11:23.650 回答
17

Laravel 5.2.27 开始,您可以通过编写条件来避免破坏链:

$query = DB::table('node')
    ->when($published, function ($q) use ($published) {
        return $q->where('published', 1);
    })
    ->when($year, function($q) use ($year) {
        return $q->where('year', '>', $year);
    })
    ->get();

要使用 Eloquent,只需交换$query = DB::table('node')Node::但要意识到如果两个条件都失败,除非在查询 db/model 或从查询本身中检查其他条件,否则您将恢复表中的所有内容。

请注意,$published并且$year必须在闭包使用的本地范围内。

您可以通过创建宏使其更加简洁和可读。请参阅:有条件地向 Laravel 的查询构建器添加指令

于 2016-08-19T18:51:20.497 回答
12

以下是完成查询的方法:

$year = 2012;
$published = true;

DB::table('node')
->where(function($query) use ($published, $year)
{
    if ($published) {
        $query->where('published', 'true');
    }

    if (!empty($year) && is_numeric($year)) {
        $query->where('year', '>', $year);
    }
})
->get( array('column1','column2') );

要查找更多信息,我建议阅读 Laravel 文档中的 Fluent 和 Eloquent。 http://laravel.com/docs/database/fluent

于 2013-01-06T06:08:37.820 回答
9

我在这里没见过。您甚至可以像这样开始查询

$modelQuery = Model::query();

然后链接其他查询命令。也许这对新人会有所帮助。

于 2019-03-15T19:51:47.653 回答
7

您可以Model::when()在 Condition 中使用,也可以创建Builder::micro()

例如

$results = Model::where('user_id', Auth::id())
    ->when($request->customer_id, function($query) use ($request){
        return $query->where('customer_id', $request->customer_id);
    })
    ->get();

如果您需要为条件创建微,那么。按照以下说明进行操作。

在您的服务提供商中编写代码

Builder::macro('if', function ($condition, $column, $operator, $value) {
    if ($condition) {
        return $this->where($column, $operator, $value);
    }

    return $this;
});

使用如下示例

$results = Model::where('user_id', Auth::id())
    ->if($request->customer_id, 'customer_id', '=', $request->customer_id)
    ->get();

参考:他们说

于 2017-04-29T05:16:59.893 回答
5

如果你需要使用 Eloquent,你可以像这样使用它,我不确定 whereNotNull 是最好的用途,但我找不到另一种方法来返回我们真正想要的空查询实例:

$query = Model::whereNotNull('someColumn');
if(x < y)   
    {
        $query->where('column1', 'LIKE', '%'. $a .'%');
    }else{
        $query->where('column2', 'LIKE', '%'. $b .'%');
    }
$results = $query->get();

这样任何关系仍然有效,例如在您的视图中您仍然可以使用

foreach($results as $result){
    echo $result->someRelationship()->someValue;
}

这里有大量关于这类东西的信息http://daylerees.com/codebright/eloquent-queries 。

于 2015-03-08T20:17:55.260 回答
4

在 Laravel > 5.2 中,您可以使用when()

$results = DB::table('orders')
    ->where('branch_id', Auth::user()->branch_id)
    ->when($request->customer_id, function($query) use ($request){
        return $query->where('customer_id', $request->customer_id);
    })
    ->get();

文档:https ://laravel.com/api/5.8/Illuminate/Contracts/Container/Container.html#method_when

博文:https ://themsaid.com/laravel-query-conditions-20160425/

于 2019-06-19T16:44:28.663 回答
0

对于雄辩的查询,我使用了以下仅在条件具有值时执行

->where(function($query) use ($value_id)
                    {

                            if ( ! is_null($value_id)) 
                                $query->where('vehicle_details.transport_type_id', $value_id);

                    })
于 2017-03-30T12:50:07.903 回答
0

我们可以这样写(更精确的方式):

$query = DB::table('node')->when($published, function ($q, $published) {
        return $q->where('published', 1);
    })->when($year, function($q, $year) {
        return $q->where('year', '>', $year);
    })->get()

Laravel 文档中没有提到。这是拉取请求

于 2019-06-10T05:38:12.160 回答