简体   繁体   English

laravel通过标签获取相关帖子

[英]laravel get related posts by tags

I want to get the related posts of a current post by tags but honestly I can't get it. 我想通过标签获取当前帖子的相关帖子,但老实说我无法获取。

I will show you my tables structure. 我将向您展示我的表格结构。

Posts Table: 帖子表:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('title');
        $table->string('slug')->unique();
        $table->text('body');
        $table->text('excerpt')->nullable();
        $table->string('stickers')->nullable();
        $table->integer('category_id')->nullable()->unsigned();
        $table->text('meta_description')->nullable();
        $table->text('meta_keywords')->nullable();
        $table->string('postimg')->nullable();
        $table->string('type')->nullable()->default('common');
        $table->boolean('published')->default(false);
        $table->softDeletes();
        $table->timestamps();
    });
}

Tags Table: 标签表:

public function up()
{
    Schema::create('tags', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->unique();
        $table->string('slug')->unique();
        $table->softDeletes();
        $table->timestamps();
    });
}

I have a pivot table to handle tags on posts and posts with those tags. 我有一个数据透视表来处理帖子上的标签以及带有这些标签的帖子。

post_tag table: post_tag表:

public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

        $table->integer('tag_id')->unsigned();
        $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
    });
}

Everything is working good, one tag have many posts and one post have many tags, is a many to many relationship. 一切工作正常,一个标签有很多帖子,一个标签有很多标签,是多对多关系。

Post Model: 帖子模型:

public function tags()
{
    return $this->belongsToMany('App\Tag');
}

Tag Model: 标签型号:

public function posts()
{
  return $this->belongsToMany('App\Post');
}

I can display all the tags on a post but i want to display the related posts by tags, when i say related posts i meaning to "the current post" that visitor is reading. 我可以在帖子上显示所有标签,但是我想按标签显示相关帖子,当我说相关帖子时,我的意思是访问者正在阅读的“当前帖子”。 Let's say that this current post have a microsoft, google, apple, cars tags, i want the related posts to those tags. 假设此当前帖子具有microsoft,google,apple,cars标签,我希望这些标签的相关帖子。 I don't know if that's possible or is easyer to do ir by categories. 我不知道这是否可能或更容易按类别进行。

News Controller logic: 新闻控制器逻辑:

Here's where i have all the logic to the post view. 这是我拥有发布视图所有逻辑的地方。

public function getSingle($slug, $id = null)
{
    $post = Post::where('slug', '=', $slug)->first();
    $topcat = Category::orderBy('created_at', 'desc')->limit(5)->get();
    $comment = Comment::find($id);

    $tags = Tag::all();
    $tags2 = array();
    foreach ($tags as $tag) {
        $tags2[$tag->id] = $tag->name;
    }

    // Previous and Next Post
    $previous = Post::where('id', '<', $post->id)->orderBy('id', 'desc')->first();
    $next = Post::where('id', '>', $post->id)->orderBy('id', 'asc')->first();

    // Related Posts Here!

    $tags3 = array();
    foreach ($post->tags as $tag) {
        $tags3[$tag->id] = $tag->name;
    }
    $related = Post::whereHas('tags', function ($query) use ($tags3) {
        $query->where('name', $tags3);
    })->get();
    // dd($related);

    return view('news.single')
            ->withPost($post)
            ->withTopcat($topcat)
            ->withTags($tags2)
            ->withComment($comment)
            ->withPrevious($previous)
            ->withNext($next)
            ->withRelated($related);
}

I did the $tags3 variable to test it that way but i didn't get what i wanted. 我做了$tags3变量来测试它,但是我没有得到想要的东西。

Thanks in advance 提前致谢

This should work: 这应该工作:

$post = Post::where('slug', '=', $slug)->first();

$related = Post::whereHas('tags', function ($q) use ($post) {
    return $q->whereIn('name', $post->tags->pluck('name')); 
})
->where('id', '!=', $post->id) // So you won't fetch same post
->get();

The $post->tags->pluck('name') line creates an array of all the tag names (that belong to the post). $post->tags->pluck('name')行创建一个包含所有标签名称(属于该帖子)的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM