3

Below is my query, and i have added some description what i want.

$recommendedByVideoId = RecommendedVideo::selectRaw('video_id,id')
                ->orderBy('played_count', 'desc')
                ->orderBy('updated_at', 'desc')
                ->get()
                ->skip(3)
                ->take(2)
                ->groupBy('video_id')

Below is my query result :

        [video_id_1] => [record1, record2, record3, record4, record5],
        [video_id_2] => [record1, record2, record3, record4, record5],
        [video_id_3] => [record1, record2, record3, record4, record5],
        [video_id_4] => [record1, record2, record3, record4, record5],
        [video_id_5] => [record1, record2, record3, record4, record5, record6, record7],

Expected Result should be something like:

        [video_id_1] => [record4, record5],
        [video_id_2] => [record4, record5],
        [video_id_3] => [record4, record5],
        [video_id_4] => [record4, record5],
        [video_id_5] => [record4, record5],

Please help me to write out query, that skip records from inner collection not from outer.

Thanks.

Vishal Ribdiya
  • 840
  • 6
  • 18

1 Answers1

1

here its already answered in statckoverflow skip frist row and take the rest

so for using this, you have to use take method with skip because with offset you give number right.

So first get the count how many rows you have in the database.

$count = Model::count()

Then $skip = 3 find limit you provide to take method $limit = $count - $skip And Now heres you final query

Model::skip($skip)->take($limit)->get();

you can use it with groupBy too

and if you don't want to change your query then get all data without skip or take and use collection method except

$data = $dataFromDataBase->except([0,1,2]);

it will automatically remove given keys from your data

Karan Sadana
  • 1,363
  • 7
  • 11