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

Calling paginate() returns error #10

Closed
leekaiwei opened this issue Jul 29, 2013 · 14 comments
Closed

Calling paginate() returns error #10

leekaiwei opened this issue Jul 29, 2013 · 14 comments

Comments

@leekaiwei
Copy link
Contributor

Doing User::paginate(1) generates stack trace:

[2013-07-29 08:36:57] log.ERROR: exception 'ErrorException' with message 'Undefined index: result' in /var/www/leaf.dev/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Builder.php:129
Stack trace:
#0 /var/www/leaf.dev/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Builder.php(129): Illuminate\Exception\Handler->handleError(8, 'Undefined index...', '/var/www/leaf.d...', 129, Array)
#1 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(943): Jenssegers\Mongodb\Builder->getFresh(Array)
#2 /var/www/leaf.dev/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Builder.php(192): Illuminate\Database\Query\Builder->get(Array)
#3 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1225): Jenssegers\Mongodb\Builder->aggregate('count', Array)
#4 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1195): Illuminate\Database\Query\Builder->count()
#5 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(213): Illuminate\Database\Query\Builder->getPaginationCount()
#6 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(184): Illuminate\Database\Eloquent\Builder->ungroupedPaginate(Object(Illuminate\Pagination\Environment), 20, Array)
#7 /var/www/leaf.dev/app/library/ArticleAPI.php(99): Illuminate\Database\Eloquent\Builder->paginate(20)
#8 /var/www/leaf.dev/app/controllers/ArticleController.php(36): app\library\ArticleAPI->latestArticles()
#9 [internal function]: ArticleController->getLatestArticles()
#10 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(138): call_user_func_array(Array, Array)
#11 /var/www/leaf.dev/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(115): Illuminate\Routing\Controllers\Controller->callMethod('getLatestArticl...', Array)
#12 /var/www/leaf.dev/bootstrap/compiled.php(4768): Illuminate\Routing\Controllers\Controller->callAction(Object(Illuminate\Foundation\Application), Object(Illuminate\Routing\Router), 'getLatestArticl...', Array)
#13 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#14 /var/www/leaf.dev/bootstrap/compiled.php(7838): call_user_func_array(Object(Closure), Array)
#15 /var/www/leaf.dev/bootstrap/compiled.php(7825): Illuminate\Routing\Route->callCallable()
#16 /var/www/leaf.dev/bootstrap/compiled.php(4779): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#17 /var/www/leaf.dev/bootstrap/compiled.php(483): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#18 /var/www/leaf.dev/bootstrap/compiled.php(472): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#19 /var/www/leaf.dev/public/index.php(49): Illuminate\Foundation\Application->run()
#20 {main} [] []
@jenssegers
Copy link
Contributor

I did not look into pagination yet, I will try to add it today.

@leekaiwei
Copy link
Contributor Author

Oh I see. Thanks.

EDIT: Actually what I want to do requires take() and skip() instead, so currently I don't need paginate() for now.

@jenssegers
Copy link
Contributor

Pagination was actually working already when I tried it.

$reports = Report::paginate(5);

Returned me 5 items, and pagination links.

@leekaiwei
Copy link
Contributor Author

Interesting, no matter where I put the call to pagination method in the chain, it wouldn't work.

@jenssegers
Copy link
Contributor

Take and skip is working as well, those are tested by unittests.

@jenssegers
Copy link
Contributor

Can I see your chain?

@leekaiwei
Copy link
Contributor Author

Yes they are working fine.

User::where('attribute', 'value')->orderBy('attribute')->paginate(number);

@jenssegers
Copy link
Contributor

I was able to execute the following without any errors:

$reports = Report::where('node', 'vmware2')->orderBy('elapsed')->paginate(5);

But when I look at your error message, the query builder started using the aggregation framework. Did you use any grouping or aggregation somewhere else or in the same query?

@leekaiwei
Copy link
Contributor Author

Not to my knowledge. If I have, then I've used it without knowing. I have 2 weeks experience of PHP/Laravel. If you say it works, then it probably does and I've done something stupid.

Off-topic: Since MongoDB can have arrays inserted into them, does this package support something like where() but for matching an item in the array? Currently doing it manually and therefore cannot use Eloquent methods, making things like skip() and take() a bit harder.

@jenssegers
Copy link
Contributor

Yes, you can just use something like this:

User::where('address.country', 'France')->get();

@leekaiwei
Copy link
Contributor Author

I think we are thinking of slightly different arrays. I was thinking of having inserted:

array('tag1', 'tag2', 'games', 'entertainment')

into a field tags. Then I would want to find all models in which its array in tags contain for example games. There isn't anything like that is there?

@jenssegers
Copy link
Contributor

You can use this query for that:

Post::where('tags', 'games')->get();

Check 92754b8

@leekaiwei
Copy link
Contributor Author

Oh I see. So that does work. Thanks very much for your help. Much appreciated.

@khungdisan
Copy link

khungdisan commented Jul 1, 2017

How to conver to array when use pagination.
I using Laravel 5.4 and jensseger mongodb 3.2

alcaeus pushed a commit to alcaeus/laravel-mongodb that referenced this issue Aug 16, 2023
…eject invalid array (mongodb#10)

The Query\Builder::whereBetween() method can be used like this:

whereBetween('date_field', [min, max])
whereBetween('date_field', collect([min, max]))
whereBetween('date_field', CarbonPeriod)

Laravel allows other formats: the $values array is flatten and the builder assumes there are at least 2 elements and ignore the others. It's a design that can lead to misunderstandings. I prefer to raise an exception when we have incorrect values, rather than trying to guess what the developer would like to do.

Support for CarbonPeriod was fixed in Laravel 10: laravel/framework#46720 because the query builder was taking the 1st 2 values of the iterator instead of the start & end dates.
alcaeus pushed a commit that referenced this issue Aug 22, 2023
…eject invalid array (#10)

The Query\Builder::whereBetween() method can be used like this:

whereBetween('date_field', [min, max])
whereBetween('date_field', collect([min, max]))
whereBetween('date_field', CarbonPeriod)

Laravel allows other formats: the $values array is flatten and the builder assumes there are at least 2 elements and ignore the others. It's a design that can lead to misunderstandings. I prefer to raise an exception when we have incorrect values, rather than trying to guess what the developer would like to do.

Support for CarbonPeriod was fixed in Laravel 10: laravel/framework#46720 because the query builder was taking the 1st 2 values of the iterator instead of the start & end dates.
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

3 participants