我试图在我的主页上显示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文件中,我试图使用这两个函数,但只有一个是有效的,因为在我的routes.php文件中,我有:
Route::get('/', ['as' => 'home', 'uses' => 'BrowseController@getLatest']);因此@foreach($posts as $post) @endif只加载getLatest而不加载getTrending
有谁可以帮我?
发布于 2016-08-15 05:44:30
您已经告诉您的路由使用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)
// ...
@endforeachhttps://stackoverflow.com/questions/38944405
复制相似问题