【问题标题】:Laravel with where eloquent builder. How to use db records values inside subquery?Laravel 与 where eloquent builder。如何在子查询中使用数据库记录值?
【发布时间】:2023-01-16 18:42:29
【问题描述】:

我想在 laravel 雄辩的子查询中使用记录字段

我试过这个

$clients = Client::with(['records' => function (Builder $query) {
   // how can i take a record fields there?
   $record = $query->first();
   $query->where('time', Carbon::now()->subMinutes(10 + $record->duration);
}])->where('profile_id', $profile->id)->get();

如何才能做到这一点?

【问题讨论】:

    标签: php laravel eloquent relationship


    【解决方案1】:

    只需使用use()

    $clients = Client::with(['records' => function (Builder $query) use ($record) {
       $query->where('time', Carbon::now()->subMinutes(10 + $record->duration);
    }])->where('profile_id', $profile->id)->get();
    

    【讨论】:

      猜你喜欢
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2020-10-30
      相关资源
      最近更新 更多