0

when i try to paginate results passed from the logged in user I get the error:

$message = Auth::user()->Notifications()->orderBy('created_at', 'desc')->get()
        ->groupBy(function($date) {
            return Carbon::parse($date->created_at)->format('M d'); // grouping by day
        })
        ->paginate(2);

Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()

what is the problem and how can I paginate my object array?

1
  • this is a spaghetti code read the documentation first. Commented Mar 13, 2015 at 9:20

2 Answers 2

1

Try this:

$message = Auth::user()->Notifications()->orderBy('created_at', 'desc')
    ->groupBy(function($date) {
        return Carbon::parse($date->created_at)->format('M d'); // grouping by day
    })->paginate(2);

the paginate method needs to be the last element in the chain like->get()

// edit: As stated here: http://laravel.com/docs/4.2/pagination

Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database manually and use Paginator::make.

This could also be helpful: http://www.jenssegers.be/blog/57/laravel-pagination-with-grouping-and-eager-loading

Sign up to request clarification or add additional context in comments.

3 Comments

i tried but got many errors: first grammar.php strtolower() expects parameter 1 to be string, object given
Hmm, you are using a reserved keyword somewhere. Have you tried paginate without the groupBy? (Just for a test)
yes without groupBy all good, but how to group my table by date and time? i need to group like this jsfiddle.net/umpr84v2 thanks
0

Of course you get the error as groupBy returns an instance of Illuminate\Support\Collection. You would need to do something like

$messages = Auth::user()->Notifications()->orderBy('created_at', 'desc')->paginate(2);

$messages->setItems($messages->groupBy(function($date) {
            return $date->created_at->format('M d'); // grouping by day
        }));

This way you set up new items in your pagination object which are grouped results from database.

7 Comments

now pagination showing and i dont get error, but notifications table emty. I dont give array maybe. its my blade jsfiddle.net/cu53m9cc thanks
Try to dump your results using messsages->getItems()->toArray() and see what is the structure. Then you should be able to figure it out.
nothing, empty too. than im dumping, got many info and browser not load to the end
So move the code which groups by your items: $test = $messages->groupBy(function($date) { return $date->created_at->format('M d'); // grouping by day }) and dump it. See if it returns correct results. put it after paginate(2);
var_dump its fine i got array, but than $test = $message->groupBy(function($date) { return $date->created_at->format('M d'); })->paginate(2); i got Call to undefined method Illuminate\Support\Collection::paginate()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.