【问题标题】:Use 2 different SQL queries in Laravel View在 Laravel View 中使用 2 个不同的 SQL 查询
【发布时间】:2016-12-21 00:41:13
【问题描述】:

我正在尝试在我的主页上显示 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

谁能帮帮我?

【问题讨论】:

    标签: php laravel laravel-4 laravel-5


    【解决方案1】:

    您已经告诉您的路由使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多