2017-10-03 2 views
0

Zuerst entschuldige ich mich für mein schlechtes Englisch. Der vorherige Code wurde nur mit den Spalten in der Tabelle 'article' sortiert. Ich möchte dem vorherigen Code eine neue Sortierung hinzufügen, aber der Wert ist in einer anderen Tabelle 'votes'. Mit Blick auf den vorherigen Code verwenden wir Laravel 'link_for_sort'. Ich denke jedoch, dass das Argument dieser Methode nicht 'Tabelle', sondern 'Spalte' ist, so dass andere Tabellen nicht verwendet werden können. Wie verwende ich Sortierung nach Wert in einer anderen Tabelle? Vielen Dank für Ihre Antwort. Vielen Dank.Wie wird mit Werten aus anderen Tabellen sortiert? (Laravel5)

* 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) 

Antwort

0

Haben Sie versucht, mit so etwas wie:

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

Entschuldigung, wenn das nicht funktioniert, hatte ich keine Zeit, es zu testen.

+0

sorry, don: t arbeiten. – User8392

Verwandte Themen