【问题标题】:I want to extract the first five data that match the where clause from Laravel relationships我想从 Laravel 关系中提取与 where 子句匹配的前五个数据
【发布时间】:2020-09-16 01:52:58
【问题描述】:

我想用 Laravel 的 Eloquent:relationship 做类似的事情,但它不起作用。

$playlist->setRelation('tags', $playlist->tags->where('privacySetting', 'public')->take(5));

它可以在没有 where 子句的情况下工作,但我想检索标签关系表中与 where 子句匹配的前 5 个数据。 我该怎么做?

Laravel 版本是 7.28.1。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    这将根据模型中的标准选择前 5 个

    $playlist = Playlist::with(['tags' => function($filter){
      return $filter->where('privacySetting', 'public')
      ->take(5);
    }])
    ->get();
    //try dd($playlist);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多