0

Hopefully a simple one, but I'm getting nowhere fast with it,

I need to perform a query in Laravel where I skip the first two records in a database,

running the query

DB::table('recent_videos')->where('user_id', $userId)->orderBy('created_at')->get()

pulls all records as expected, but when I use

DB::table('recent_videos')->where('user_id', $userId)->orderBy('created_at')->skip(2)->get();

I get the following error:

Your Video count not be created due to this error: SQLSTATE[42000]: Syntax error or access
 violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 'offset 2' at line 1 
(SQL: select * from `recent_videos` where `user_id` = 1 order by `created_at` asc offset 2)

I get that it's related to offset 2 but I can't work out how to fix it,

Thanks!

Shadow
  • 33,525
  • 10
  • 51
  • 64
WillMaddicott
  • 512
  • 6
  • 20
  • Put the `skip` after the `get`. I.e. `DB::table('recent_videos')->where('user_id', $userId)->orderBy('created_at')->get()->skip(2);` – Lysander Cox Feb 23 '21 at 21:51
  • Or use `take(n)` with a very big value as suggested here https://stackoverflow.com/questions/2827029/mysql-skip-first-10-results – M Khalid Junaid Feb 24 '21 at 06:21

0 Answers0