I have the following Eloquent query (This is a simplified version of a query which consists of of more wheres and orWheres hence the apparent roundabout way of going about this - the theory is what's important):

$start_date = //some date;

$prices = BenchmarkPrice::select('price_date', 'price')
->orderBy('price_date', 'ASC')
->where('ticker', $this->ticker)
->where(function($q) use ($start_date) {

    // some wheres...

    $q->orWhere(function($q2) use ($start_date){
        $dateToCompare = BenchmarkPrice::select(DB::raw('min(price_date) as min_date'))
        ->where('price_date', '>=', $start_date)
        ->where('ticker', $this->ticker)
        ->pluck('min_date');

        $q2->where('price_date', $dateToCompare);
    });
})
->get();

正如你所看到的,我知道最早的日期是在我start_date岁那年或之后.这将导致运行一个单独的查询来获取该日期,然后将该日期用作主查询中的参数.有没有一种方法可以Eloquent 地将查询嵌入到一起,形成一个子查询,从而只调用一个数据库调用,而不是两个?

Edit:

As per @Jarek's answer this is my query:

$prices = BenchmarkPrice::select('price_date', 'price')
->orderBy('price_date', 'ASC')
->where('ticker', $this->ticker)
->where(function($q) use ($start_date, $end_date, $last_day) {
    if ($start_date) $q->where('price_date' ,'>=', $start_date);
    if ($end_date) $q->where('price_date' ,'<=', $end_date);
    if ($last_day) $q->where('price_date', DB::raw('LAST_DAY(price_date)'));

    if ($start_date) $q->orWhere('price_date', '=', function($d) use ($start_date) {

        // Get the earliest date on of after the start date
        $d->selectRaw('min(price_date)')
        ->where('price_date', '>=', $start_date)
        ->where('ticker', $this->ticker);                
    });
    if ($end_date) $q->orWhere('price_date', '=', function($d) use ($end_date) {

        // Get the latest date on or before the end date
        $d->selectRaw('max(price_date)')
        ->where('price_date', '<=', $end_date)
        ->where('ticker', $this->ticker);
    });
});
$this->prices = $prices->remember($_ENV['LONG_CACHE_TIME'])->get();

orWhere个块导致查询中的所有参数突然变得不带引号.例如,WHEREPrice_date>= 2009-09-07.当我删除orWheres时,查询运行正常.这是为什么呢?

推荐答案

下面是执行子查询的方法,其中:

$q->where('price_date', function($q) use ($start_date)
{
   $q->from('benchmarks_table_name')
    ->selectRaw('min(price_date)')
    ->where('price_date', '>=', $start_date)
    ->where('ticker', $this->ticker);
});

遗憾的是,orWhere需要显式提供$operator,否则将引发错误,因此在您的示例中:

$q->orWhere('price_date', '=', function($q) use ($start_date)
{
   $q->from('benchmarks_table_name')
    ->selectRaw('min(price_date)')
    ->where('price_date', '>=', $start_date)
    ->where('ticker', $this->ticker);
});

EDIT: You need to specify from in the closure in fact, otherwise it will not build correct query.

Laravel相关问答推荐

子目录中具有Vue实例的Laravel不起作用

未将Laravel Blade属性传递给组件或类

在Laravel Livewire中,如果一个输入基于另一个唯一的字段是唯一的,如何判断验证?

如何在 Laravel 中使用 return 停止 Trait php 的执行

在 Laravel 中排序的集合在 Vue 中突然不再排序

laravel vue 惯性分页器删除上一个和下一个链接

如何通过 Controller 访问 Laravel 中 Model 的值?

如何在 Laravel Passport 中获取刷新令牌?

错误try 访问 null 类型值的数组偏移量laravel 5.8.26 Php 7.4.2

具有实时和 WebSockets 的 Angular2 + Laravel

在 VSCode 上调试 Laravel 应用程序

如何创建具有关系的 Eloquent 模型?

如何更改 Handlebars.js 的默认分隔符?

如何在 Laravel 5.1 中改变环境?

如何在 Laravel 5 中按键获取所有缓存项目的列表?

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

在 Laravel 中按用户名查找用户

Elastic search全文与mysql全文?

Laravel 单元测试依赖注入

如何使用 Laravel 迁移在 Mysql 列中添加注释