如何使用其他表中的值进行排序? (Laravel5)

时间:2017-10-03 08:44:24

标签: php laravel sorting

首先,我为我可怜的英语道歉。 前面的代码只使用'article'表中的列进行排序。 我想在前面的代码中添加一个新的排序,但是值在另一个表'votes'中。 看看上面的代码,我们使用的是laravel的'link_for_sort'。但是,我认为这种方法的论点不是'table'而是'column',所以不能使用其他表。 如何在另一个表中按值应用排序? 谢谢你的回复。感谢。

* project.php

'sorting' => [
    'view_count' => 'view',
    'created_at' => 'date',
],

'sorting_vote' => [
    'up' => 'score',
],

* index.blade.php

<div class="btn-group sort__article">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <i class="fa fa-sort"></i>
    {{ trans('forum.articles.sort') }}
    <span class="caret"></span>
  </button>

  <ul class="dropdown-menu" role="menu">
    // table: article, column: view_count,created_at
    @foreach(config('project.sorting') as $column => $text)
      <li {!! request()->input('sort') == $column ? 'class="active"' : '' !!}>
        {!! link_for_sort($column, $text) !!}
      </li>
    @endforeach

    // table: votes, column: up
    @foreach(config('project.sorting_vote') as $column => $text)
      <li {!! request()->input('sort') == $column ? 'class="active"' : '' !!}>
        {!! link_for_sort($column, $text) !!}
      </li>
    @endforeach
  </ul>
</div>

* Article.php

public function votes()
{
    return $this->hasMany( Vote::class );
}




MariaDB [root]> desc articles;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id    | int(10) unsigned | NO   | MUL | NULL    |                |
| title      | varchar(191)     | NO   | MUL | NULL    |                |
| content    | text             | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| thumbnail  | varchar(191)     | YES  |     | NULL    |                |
| view_count | int(10)          | NO   |     | 0       |                |
+------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

MariaDB [root]> desc votes;
+------------+------------------+------+-----+-------------------+-----------------------------+
| Field      | Type             | Null | Key | Default           | Extra                       |
+------------+------------------+------+-----+-------------------+-----------------------------+
| id         | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| user_id    | int(10) unsigned | NO   | MUL | NULL              |                             |
| article_id | int(10) unsigned | NO   | MUL | NULL              |                             |
| up         | tinyint(4)       | YES  |     | NULL              |                             |
| down       | tinyint(4)       | YES  |     | NULL              |                             |
| voted_at   | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

你尝试过类似的事情吗?

Article::with('votes')->orderBy('votes.up', 'DESC')->paginate(10)

道歉,如果这不起作用,我没有时间去测试它。