【问题标题】:How to use LIMIT clause with offset and number of rows in Laravel query builder?如何在 Laravel 查询生成器中使用带有偏移量和行数的 LIMIT 子句?
【发布时间】:2014-09-25 16:54:05
【问题描述】:

我在 Laravel 中有一个类似于下面的声明:

$tiles = Tile::with('comments', 'user', 'category', 'ratings')
    ->take($startRow, 15)
    ->get();
return Response::json($tiles);

但这只是限制了返回的行数,并没有考虑偏移量。我尝试用select(DB::raw('LIMIT(' . $startRow . ',15)')) 替换take 语句,但这会产生SQL 语法错误。

那么如何使用 Laravel 的查询构建器在我的 SQL 查询末尾添加 LIMIT :startRow, 15 子句?

【问题讨论】:

    标签: laravel-4 query-builder


    【解决方案1】:

    我建议你试试这个。 skip() 方法用于偏移

    $tiles = Tile::with('comments', 'user', 'category', 'ratings')
                 ->take(15)
                 ->skip($startRow)
                 ->get();
    return Response::json($tiles);
    

    阅读更多关于skip的信息。

    【讨论】:

    • 是的,这行得通!我不敢相信我没有注意到 skip() 函数,它就在文档 D 中:
    猜你喜欢
    • 2010-11-17
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2017-03-29
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多