Laravel get() 后如何使用 limit 和 offset

How to use limit and offset after get() in Laravel

我想获取第一次连接到数据库时的所有记录,然后获取这些记录的数量。

$journeyItems = JourneyItem::whereIn('as_journey_id', $journeyIds->toArray())
    ->whereHas('content', function (Builder $query) {
        $query->whereIn('as_content_type_id', [6, 7]);
    })->get();
$totalRecords = $journeyItems->count(); // count=15
$journeyItems = $journeyItems->offset($start)
    ->limit($limit);

dd(journeyItems); // Error 

Illuminate\Database\Eloquent\Collection::offset does not exist As a result: It didnt work

我该如何编辑?

我猜下面的方法不是正确的方法

           $journeyItems = JourneyItem::whereIn('as_journey_id', $journeyIds->toArray())
                ->whereHas('content',function(Builder $query) {
                    $query->whereIn('as_content_type_id', [ 6, 7]);
                })
                ->offset($start)
                ->limit($limit)
                ->get();
            $totalRecords = JourneyItem::whereIn('as_journey_id', $journeyIds->toArray())
                ->whereHas('content',function(Builder $query) {
                    $query->whereIn('as_content_type_id', [ 6, 7]);
                })
                ->count();

$journeyItems是集合,所以用skip and takeoffsetlimit 是查询生成器方法。

$journeyItems = $journeyItems->skip($start)->take($limit);

或者,您可以使用 slice:

$journeyItems = $journeyItems->slice($start, $limit);