douliao7930 2018-05-14 01:29
浏览 41
已采纳

laravel按标签获取相关帖子

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:

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. 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.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongyuan7110 2018-05-14 01:41
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题