cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.46k stars 517 forks source link

Pagination for messages and/or threads #225

Closed MrMooky closed 7 years ago

MrMooky commented 7 years ago

Hello again. I tried to implement a pager for the messages (->paginate(10)) but that does not work. Is there any way to limit the messages output within the show-action?

antonkomarev commented 7 years ago

@MrMooky Could you show more detailed code block?

MrMooky commented 7 years ago

@a-komarev I have no working code, thus I'm asking. The following code does not support a pager:

That's the way a thread is called within the show-action, that automatically fetches the messages the thread it is containing:

$thread = Thread::findOrFail($id);

The code should be something like this: $thread = DB::table('threads')->messages->simplePaginate(10);

But that does not work.

antonkomarev commented 7 years ago
$thread = Thread::findOrFail($id);
$thread->messages()->simplePaginate(10);

Wouldn't this work?

MrMooky commented 7 years ago

I've tried the code you suggested. Did not throw an error instantly... but when adding the needed code to my blade file, I got

Method links does not exist.

That's my current blade code, as provided here.

@each('messenger.partials.messages', $thread->messages, 'message')
{{ $thread->messages->links() }}
antonkomarev commented 7 years ago

Read about Laravel pagination in documentation, please.

$messages = $thread->messages()->paginate(10);

And use $messages collection in your views instead of fetching messages from thread again.

MrMooky commented 7 years ago

Ok, that worked. I was just using the example code that was provided here and figured it should work with this.

Thank you very much.