我正在尝试在我的主页上显示2列不同的
我有BrowseController.php
个文件:
/**
* @return mixed
*/
public function getTrending()
{
$posts = $this->posts->getTrending(null, ['category' => Input::get('category'), 'timeframe' => Input::get('timeframe')]);
return View::make('post.list')->with('title', t('Trending'))->with('posts', $posts);
}
/**
* @return mixed
*/
public function getLatest()
{
$posts = $this->posts->getLatest(null, ['category' => Input::get('category'), 'timeframe' => Input::get('timeframe')]);
$title = t('Latest');
return View::make('post.list', compact('title', 'posts'));
}
和PostsRepository.php
文件:
public function getTrending($type = null, $param = [])
{
isset($param['timeframe']) ? $param['timeframe'] = $param['timeframe'] : $param['timeframe'] = 'month';
$posts = $this->posts($type, $param)->with('comments', 'votes', 'category', 'user', 'votes.user')
->leftJoin('votes', 'posts.id', '=', 'votes.post_id')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->select('posts.*', DB::raw('count(votes.post_id)*5 as popular'))
->groupBy('posts.id')->with('user')->orderBy('popular', 'desc');
$posts = $posts->paginate(perPage());
return $posts;
}
public function getLatest($type = null, $param = [])
{
$posts = $this->posts($type, $param)->with('comments', 'votes', 'category', 'user', 'votes.user')->orderBy('approved_at', 'desc')->paginate(perPage());
return $posts;
}
在我的刀片php文件中,我试图使用这2个函数但只有一个正在工作,因为在我的routes.php
文件中我有这个:
Route::get('/', ['as' => 'home', 'uses' => 'BrowseController@getLatest']);
所以@foreach($posts as $post) @endif
仅加载getLatest
而不加载getTrending
任何人都可以帮助我吗?
答案 0 :(得分:0)
您已告知您的路由使用getTrending()
控制器方法,但getLatest()
调用完全存在于另一种方法中。如果要在同一页面中显示最新和趋势帖子,请将两个方法调用合并为一个控制器方法:
// BrowseController.php
public function getLatestAndTrending()
{
$trendingPosts = $this->posts->getTrending(null, ['category' => Input::get('category'), 'timeframe' => Input::get('timeframe')]);
$latestPosts = $this->posts->getLatest(null, ['category' => Input::get('category'), 'timeframe' => Input::get('timeframe')]);
return View::make('post.list')
->with('title', t('New and trending'))
->with('trendingPosts', $trendingPosts)
->with('latestPosts', $latestPosts);
}
将路线更改为指向getLatestAndTrending
控制器方法:
Route::get('/', ['as' => 'home', 'uses' => 'BrowseController@getLatestAndTrending']);
然后在您看来,可以分别迭代趋势和最新帖子:
@foreach($trendingPosts as $post)
// ...
@endforeach
@foreach($latestPosts as $post)
// ...
@endforeach