首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel View中使用2个不同的SQL查询

在Laravel View中使用2个不同的SQL查询
EN

Stack Overflow用户
提问于 2016-08-15 00:50:49
回答 1查看 45关注 0票数 1

我试图在我的主页上显示2个不同的栏目

我有一个BrowseController.php文件:

代码语言:javascript
复制
 /**
 * @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文件:

代码语言:javascript
复制
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文件中,我有:

代码语言:javascript
复制
Route::get('/', ['as' => 'home', 'uses' => 'BrowseController@getLatest']);

因此@foreach($posts as $post) @endif只加载getLatest而不加载getTrending

有谁可以帮我?

EN

回答 1

Stack Overflow用户

发布于 2016-08-15 05:44:30

您已经告诉您的路由使用getTrending()控制器方法,但是getLatest()调用存在于完全不同的方法中。如果您想在同一页中显示最新和最新的帖子,请将两个方法调用合并为一个控制器方法:

代码语言:javascript
复制
// 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控制器方法:

代码语言:javascript
复制
Route::get('/', ['as' => 'home', 'uses' => 'BrowseController@getLatestAndTrending']);

然后在你的视图中,可以分别迭代热门和最新的帖子:

代码语言:javascript
复制
@foreach($trendingPosts as $post)
    // ...
@endforeach

@foreach($latestPosts as $post)
    // ...
@endforeach
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38944405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档