1
votes

I have three tables in laravel5.6 vuejs2 application

  1. Feed (id, message, created_at, updated_at, created_by, updated_by)
  2. Classifieds (id, category_id, title, description, keywords, created_at, updated_at, created_by, updated_by)
  3. News (id, title, subtitle, content, keywords, created_at, created_by, updated_at, updated_by).

I want to create a backend like facebook where i can list all the latest posts as stories. In this case i want to post feed, classifieds and news with load more button. How to achieve this?

$feed = Feed::latest()->paginate(10);
$classifieds = Classifieds::latest()->paginate(10);
$news = News::latest()->paginate(10);

$stories = array_merge(['feed', 'classifieds', 'news']);

this doesn't work and i know this is not the right way to do. I'm a learner in laravel and did not find much help on this.

Could anyone tell me how to do this so that i can list all the three tables which are not related on the dashboard page as latest stories.

1

1 Answers

3
votes

Creating ModelCollection is cool way to do that.

$collection = new ModelCollection();

$feed = Feed::latest();
$classifieds = Classifieds::latest();
$news = News::latest();

$stories = $collection->merge($feed)->merge($classifieds)->merge($news);

EDIT: You can do pagination things like that.

$page = $request->get('page');
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

$collection = new ModelCollection();
$feed = Feed::latest();
$classifieds = Classifieds::latest();
$news = News::latest();

$stories = $collection->merge($feed)->merge($classifieds)->merge($news);
$items = new LengthAwarePaginator($stories->forPage($page, 30), $result->count(), 30, $page,['path' => '/search']);

LengthAwarePaginator can make this, usage is

LengthAwarePaginator(currentPageItems, collection_count, perPage, currentPage)

I hope it was help. Edit section could not work, i just told it because lengthawarepaginator can gives you an idea.