获取 laravel 中具有特定标签的所有数据
Get all the data with specific tag in laravel
我正在为 post
和 tags
到 post_tags
使用 eloquent 关系 。我想要做的是在我的前端视图中使用特定的 tags
制作 posts
的部分。就像我有一个部分 1
它应该有标签 "new"
的所有帖子,部分 2
应该有标签 "old"
等的所有帖子。我希望所有这些发生在同一个视图中。
Post型号
public function tags()
{
return $this->belongsToMany('App\Tag', 'post_tag');
}
标签模型
public function posts()
{
return $this->belongsToMany('App\Post', 'post_tag');
}
控制器
public function index()
{
$posts = Post::all();
return view('frontend.index')->withPosts($posts);
}
请帮帮我:)
要获取标签名称为 new
的所有帖子,请执行以下操作:
Post::whereHas('tags', function ($q) {
$q->where('name', 'new');
})->get();
您可以通过加载帖子获取所有标签:
public function index()
{
$tags = Tag::with('posts')->get();
return view('frontend.index')->withTags($tags);
}
在视图中你可以这样做:
@foreach ($tags as $tag)
<h2>Tag : {{ $tag->name }}</h2>
@foreach ($tag->posts as $post)
<p>Post : {{ $post->title }}</p>
@endforeach
@endforeach
获取带有标签的帖子:
public function index()
{
$tagName = 'new';
$posts = Post::whereHas('tags', function ($q) use($tagName) {
$q->where('name', $tagName);
})->get();
return view('frontend.index')->withTagName($tagName)->withPosts($posts);
}
在视图中您可以这样做:
<h2>Tag : {{ $tagName }}</h2>
@foreach ($posts as $post)
<p>Post : {{ $post->title }}</p>
@endforeach
如果你想从视图中进行查询,你可以这样做(但这不是一个很好的做法,因为视图只是为了查看内容而不是从数据库中获取它 ) :
<?php foreach ((\App\Post::whereHas('tags', function ($q) use($tagName) {
$q->where('name', $tagName);
})->get()) as $post) { ?>
<p>Post : {{ $post->title }}</p>
<?php } ?>
不是完美的答案
您可以通过两种方式处理这种情况
1- send two objects from controller to view seprate one for only new tag and second without new tag
2- 你可以在前端使用条件处理这种情况
@if($user->tag == "new")
{$user->show}
@endif
我正在为 post
和 tags
到 post_tags
使用 eloquent 关系 。我想要做的是在我的前端视图中使用特定的 tags
制作 posts
的部分。就像我有一个部分 1
它应该有标签 "new"
的所有帖子,部分 2
应该有标签 "old"
等的所有帖子。我希望所有这些发生在同一个视图中。
Post型号
public function tags()
{
return $this->belongsToMany('App\Tag', 'post_tag');
}
标签模型
public function posts()
{
return $this->belongsToMany('App\Post', 'post_tag');
}
控制器
public function index()
{
$posts = Post::all();
return view('frontend.index')->withPosts($posts);
}
请帮帮我:)
要获取标签名称为 new
的所有帖子,请执行以下操作:
Post::whereHas('tags', function ($q) {
$q->where('name', 'new');
})->get();
您可以通过加载帖子获取所有标签:
public function index()
{
$tags = Tag::with('posts')->get();
return view('frontend.index')->withTags($tags);
}
在视图中你可以这样做:
@foreach ($tags as $tag)
<h2>Tag : {{ $tag->name }}</h2>
@foreach ($tag->posts as $post)
<p>Post : {{ $post->title }}</p>
@endforeach
@endforeach
获取带有标签的帖子:
public function index()
{
$tagName = 'new';
$posts = Post::whereHas('tags', function ($q) use($tagName) {
$q->where('name', $tagName);
})->get();
return view('frontend.index')->withTagName($tagName)->withPosts($posts);
}
在视图中您可以这样做:
<h2>Tag : {{ $tagName }}</h2>
@foreach ($posts as $post)
<p>Post : {{ $post->title }}</p>
@endforeach
如果你想从视图中进行查询,你可以这样做(但这不是一个很好的做法,因为视图只是为了查看内容而不是从数据库中获取它 ) :
<?php foreach ((\App\Post::whereHas('tags', function ($q) use($tagName) {
$q->where('name', $tagName);
})->get()) as $post) { ?>
<p>Post : {{ $post->title }}</p>
<?php } ?>
不是完美的答案
您可以通过两种方式处理这种情况
1- send two objects from controller to view seprate one for only new tag and second without new tag
2- 你可以在前端使用条件处理这种情况
@if($user->tag == "new")
{$user->show}
@endif