Mysql 如何使用Laravel中雄辩的查询生成器获取最后这么多条目(跳过)

Mysql 如何使用Laravel中雄辩的查询生成器获取最后这么多条目(跳过),mysql,laravel,laravel-4,eloquent,Mysql,Laravel,Laravel 4,Eloquent,我正在尝试使用Eloquent在Laravel中编写一个查询,但只想要其中的最后5个字段。以下是查询: public static function past_profile_fan_likes($id) { $latest_profile_fan_likes = DB::table('fanartists') ->join('artists', 'fanartists.artist_id', '=', 'artists.id

我正在尝试使用Eloquent在Laravel中编写一个查询,但只想要其中的最后5个字段。以下是查询:

    public static function past_profile_fan_likes($id) {
        $latest_profile_fan_likes = DB::table('fanartists')
                    ->join('artists', 'fanartists.artist_id', '=', 'artists.id')
                    ->orderBy('fanartists.created_at', 'DESC')
                    ->skip(4)
                    ->where('fanartists.fan_id', '=', $id)
                    ->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
                    ->get();


        return $latest_profile_fan_likes;

    }
当我调用此命令时,我得到以下错误:

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 4' at line 1 (SQL: select `artists`.`id`, `artists`.`fbid`, `artists`.`stage_name`, `artists`.`city`, `artists`.`state`, `artists`.`image_path`, `artists`.`description` from `fanartists` inner join `artists` on `fanartists`.`artist_id` = `artists`.`id` where `fanartists`.`fan_id` = ? order by `fanartists`.`created_at` desc offset 4) (Bindings: array ( 0 => '1', ))

我有什么地方做错了吗?可能是skip的用法有问题?谢谢你的帮助

这看起来更像是我不太熟悉的fluent。但是,如果您使用Eloquent定义了您的模型和关系,那么如果您只是想获取最后5条记录,您可以颠倒顺序并获取(5):
像这样


$latest_profile_fan_likes=fanartists::with('artists')->where('fan_id','=',$id)->orderBy('created_at','ASC')->take(5)->get('id','fbid'…);

您需要添加一个
take
查询,以便它添加一个
LIMIT
查询并将其转换为正确的语法

DB::table('fanartists')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->skip(4)
->take(100)
->where('fanartists.fan_id', '=', $id)
->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
->get();

如果使用偏移量,即使不想限制,也需要提供限制。请参见此处:

我不想要最后5条记录,我想要前4条之后的所有记录。所以记录5到多少。啊,好的。您的skip()在where()前面的逻辑中指示。根据您得到的错误(偏移量),我将其移动到where子句之后。是否有方法提供足够大的限制,使其能够处理任意数量的条目?