Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eloquent ORM - How to use SQL_CALC_FOUND_ROWS #12469

Closed
msieprawski opened this issue Feb 25, 2016 · 2 comments
Closed

Eloquent ORM - How to use SQL_CALC_FOUND_ROWS #12469

msieprawski opened this issue Feb 25, 2016 · 2 comments

Comments

@msieprawski
Copy link

Hey,

Let's say that I've got following code:

$users = User::select(['users.id'])
    ->leftJoin('posts', 'users.id', '=', 'posts.author_id')
    ->skip(15)
    ->take(15)
    ->get();

And I want to count all available rows (ignoring the limit) so I want to add SQL_CALC_FOUND_ROWS. However when I add it to the select:

$users = User::select([DB::raw('SQL_CALC_FOUND_ROWS'), 'users.id'])
    ->leftJoin('posts', 'users.id', '=', 'posts.author_id')
    ->skip(15)
    ->take(15)
    ->get();

Laravel adds "," between columns so it ends with invalid query. Is there any way to mix SQL_CALC_FOUND_ROWS with selected columns? Please advice.

I know that I could use just count statement. But in this specific example I'd rather use SQL_CALC_FOUND_ROWS.

Thanks!

@JayBizzle
Copy link
Contributor

This?

$users = User::select([DB::raw('SQL_CALC_FOUND_ROWS users.id')])
    ->leftJoin('posts', 'users.id', '=', 'posts.author_id')
    ->skip(15)
    ->take(15)
    ->get();

@msieprawski
Copy link
Author

Well... KISS. That's a good approach, however I thought that there is a "cleaner" way to do it :)
Anyway, thanks! That worked of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants