是否可以在select中添加laravel paginate()函数?

时间:2016-05-12 08:30:40

标签: php laravel

这是我的代码,是否可以使用select查询附加laravel paginate函数paginate(15)? 我问,因为我在查询下运行时出错。 Call to a member function paginate() on array。 你能帮我解决这个问题吗?

$select = DB::connection('mysql2')->select("SELECT c.company, c.id FROM register_companies c 
            INNER JOIN register_cities rc ON rc.id = c.register_city_id
            WHERE rc.title = '" . urldecode($city) . "' 
            AND UPPER(SUBSTR(c.company,1, 1)) NOT RLIKE '^[A-Z]' 
            AND c.status = 'online' 
            ORDER BY c.company")->paginate(15);

1 个答案:

答案 0 :(得分:1)

要获得分页,您必须使用Laravel的查询构建器,因此您的查询将如下所示:

DB::connection("mysql2")
                ->table("register_companies")
                ->join("register_cities", "register_cities.id", "=", "register_companies.register_city_id")
                ->whereRaw("register_cities.title=? 
                            and UPPER(SUBSTR(register_companies.company,1, 1)) NOT RLIKE '^[A-Z]' 
                            and register_companies.status = 'online'", [urldecode($city)])
                ->orderBy("register_companies.company")
                ->selectRaw("register_companies.company, register_companies.id")
                ->paginate(15);