Laravel Undefined offset when using Skip() in database query

  database, eloquent, laravel, mysql, php

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,


Source: Laravel

Leave a Reply