【问题标题】:Laravel, how to add on the fly condition to query with query builderLaravel,如何动态添加条件以使用查询生成器进行查询
【发布时间】:2014-10-21 13:21:39
【问题描述】:

这就是问题所在:

如何在联合条件产生之前向查询添加限制或偏移等条件?

这是我尝试过的:

$query = DB::table('table_name');
$query->where(condition);
$query->orWhere(condition);

$query_no_union_yet = $query->select(field list);

$query->union(another_table);
$result_without_limit = $query->get();

$query_with_limit = $query_no_union_yet->limit(10);
$query_with_limit->union(another_table);
$result_with_limit = $query_with_limit->get();

真正的代码很长很复杂,但情况就是这样。 当到达$query_with_limit->union(another_table); 行时,我在我之前创建的那个上添加了一个联合,这不是我想要的。只有在它给我结果之后,我才能在第一个查询中添加限制,并以这种方式获得 2 组结果。

有可能,有什么想法吗?

【问题讨论】:

    标签: php mysql laravel union query-builder


    【解决方案1】:

    您可以clone the object,这样您就有两个查询构建器,它们是两个独立的实例。

    $query_no_union_yet = (clone) $query->select(field list);
    

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 2013-05-30
      • 2018-02-16
      • 2021-09-07
      • 2016-11-16
      • 2019-04-25
      • 2018-06-23
      • 2013-06-24
      • 2021-07-02
      相关资源
      最近更新 更多