laravel/laravel — Issue #254: Can't use paginate() inside many-to-many query
Author: ghsehn
Created: 2012-01-20 16:17:33+00:00
State: open

Let's explain with an example. I have a query using MySQL + Eloquent that gets all posts (I am developing an online game website) from a category:

```php
$games = Category::where_slug($slug)->games();
```

However, I can't paginate it. When I call $games->paginate() I get the following error:

> 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 'SELECT `games`.* FROM `games` INNER JOIN `categories_games` ON `games`.`id` = `c' at line 1

After searching the file that generates the SQL queries, I changed **select** function on **Laravel\Database\Grammars\Grammar** at **/laravel/database/grammars/grammar.php** to show the queries, and I've got this:

> SELECT COUNT(*) SELECT `games`.* FROM `games` INNER JOIN `categories_games` ON `games`.`id` = `categories_games`.`game_id` WHERE 1 = 1 AND `categories_games`.`category_id` = ?

It appears the counting query is not complete, and the string is not being cleared before the next query.