Laravel Eloquent skip n, take all?

后端 未结 3 1535
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 23:19

I\'ve noticed that in Laravel when chaining a skip() you must also use take() as well. I want to skip the first n rows but take the rest. The take

3条回答
  •  醉酒成梦
    2021-01-07 23:40

    If you're using MySQL and you don't want to have 2 queries to get the max rows, you may use the PHP maximum INT value as take() parameter e.g. take(PHP_INT_MAX).

    It's because Laravel Query Grammar casts the limit to integer. So you can't use a larger number than that.

    From MySQL documentation on LIMIT:

    To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter.

    For example:

    \App\User::skip(10)->take(PHP_INT_MAX)->get();
    

提交回复
热议问题