0
我堆放在我的Laravel項目中,帖子通過標籤選擇,按降序排序。我想通過特定標籤顯示帖子。我使用manyToMany
關係創建了Post
和Tag
模型。我還創建了一個數據透視表post_tag
與post_id
和tag_id
。在PostsController創建tags()
方法:Laravel 5.2 - 按標籤和順序選擇帖子降
public function tags($tagid)
{
$tag = $this->tags->find($tagid);
return view('frontend.posts.tag', compact('tag'));
}
和循環它的觀點:
@foreach($tag->posts as $post)
{{ $post->title }}
....
@endforeach
這對我的作品,但我需要按posts.created_at
帖子降序排列。如何使用Tag :: find($ id)進行查詢,但通過Posts表中的posts.created_at字段進行排序?這是我的郵政和標籤型號多對多關係:
class Post extends Model
{
.....
public function tags()
{
return $this->belongsToMany(Tag::class);
}
.....
和
class Tag extends Model
{
protected $fillable = ['name'];
public function posts()
{
return $this->belongsToMany(Post::class);
}
}
請,如何訂購在表posts
通過created_at
列降序排列由特定的標籤選中的帖子?我也希望按降序使用該查詢中的paginate()metohd。請幫忙。