", 5); $builderB = User::where("color", "blue"-6ren">
gpt4 book ai didi

laravel - 有没有办法结合 Laravel 查询生成器条件?

转载 作者:行者123 更新时间:2023-12-05 09:15:58 24 4
gpt4 key购买 nike

假设我有 2 个查询构建器:

$builderA = User::where("happiness", ">", 5);
$builderB = User::where("color", "blue");

除了输入一个新的构建器之外,还有什么方法可以组合这 2 个构建器,从而生成如下所示的构建器?

$combined = User::where("happiness", ">", 5)->where("color", "blue");

最佳答案

对于更复杂的情况,您可以使用tapmergeWheres:

$where = User::where('a', 1)->where('b', '>', 2)->where('c', '<', 2)->where('d', '<>', 2);

$combined = User::where('e', 5)->tap(function (Builder $builder) use ($where) {
$builder->mergeWheres($where->wheres, $where->getBindings());
});

Laravel 5.6 添加了几种处理子查询的方法:

关于laravel - 有没有办法结合 Laravel 查询生成器条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51297078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com