分解特定博客列中的唯一标签值 table

Explode unique tags values from specific column of blogs table

我有 table 名为博客,其中我有标签,用户可以在其中存储他们自己的自定义多个标签。

Blogs Table :
Id
Name
Tags

我使用内爆函数将它们存储为:

$tags = implode(',', $tags);

现在我想分解所有我厌倦但没有成功的值我也想分解这些值作为唯一不重复的值。

这是我试过的查询

$blogs_tag = \App\Models\Blog::select('tags')->where('status' , '=' ,'active')->get();
$tags = explode("," ,$blogs_tag);

@foreach ($tags as $tag)            
  <a class="item active" href="#">{{$tag}}</a>
@endforeach

现在当我 DD 单个值时它给出以下输出:

  "[{"tags":"new"}"

正如评论中指出的那样;

$blogs_tag = Blog::select('tags')->where('status' , '=' ,'active')->get();

Returns CollectionBlog 模型,不是所有 Blog 模型的所有 tags,所以在你的 @foreach 循环中,您不是在循环所有 tags,而是循环从查询返回的所有 Blog 模型。

一个选项,如果你只想要 tags,你可以循环你的结果。例如:

$tags = [];

$blog_tags->each(function ($blog) use (&$tags) {
    $tags = array_merge($tags, explode(',', $blog->tags));
});

以上假设您希望所有标签作为一个数组,但基本上它遍历您的 $blog_tags 结果获取 $tags 属性并使用 explode 分隔逗号在将其与 $tags 数组中的任何先前迭代组合之前的字符串。

要删除重复项,您可以使用 array_unique