0
Laravel5.1,關係,EloquentORM,多對多,排序
<table border="true">
<tr>
<th style="width:100px">Table</th>
<th colspan="3" style="width:300px">Columns</th>
</tr>
<tr>
<td style="width:100px">Articles</td>
<td style="width:100px">id</td>
<td colspan="2" style="width:200px; color: #aaaaaa">other dependent variables</td>
</tr>
<tr>
<td style="width:100px">Article_Tag</td>
<td style="width:100px">id</td>
<td style="width:100px">article_id</td>
<td style="width:100px">tag_id</td>
</tr>
<tr>
<td style="width:100px">Tags</td>
<td style="width:100px">id</td>
<td style="width:100px">name</td>
<td style="width:100px"></td>
</tr>
</table>
然後,我想通過標籤表的[名],按文章表。 我嘗試用下面的閉包急切加載,但它沒有工作。 模型文章和標籤只是由belongsToMany, 相互連接,我成功地輸出他們的數據,但他們沒有排序。外面,我沒有給getTag任何論點(當我給了論點,他們沒有'牛逼的[名]收窄要麼)
use App\Article;
use App\Tag;
...
public function __construct(Article $article)
{
$this->article = $article;
}
...
public function getTag($tag = NULL)
{
$flag = isset($tag);
$articles = $this->article->orderBy('created_at', 'desc')->with([
'tags' => function($query) use ($flag, $tag){
($flag)
? $query->where('name', $tag)
: $query->orderBy('name');
}
])->paginate(15);
我兩個launguage multiposted(另一個是日本),因爲我快點。 http://ja.stackoverflow.com/questions/27838/laravel5-1-%E3%83%AA%E3%83%AC%E3%83%BC%E3%82%B7%E3%83%A7% E3%83%B3-eloquentorm-%E5%A4%9A%E5%AF%BE%E5%A4%9A-%E3%82%BD%E3%83%BC%E3%83%88 –