簡體   English   中英

laravel通過標簽獲取相關帖子

[英]laravel get related posts by tags

我想通過標簽獲取當前帖子的相關帖子,但老實說我無法獲取。

我將向您展示我的表格結構。

帖子表:

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();
    });
}

標簽表:

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

我有一個數據透視表來處理帖子上的標簽以及帶有這些標簽的帖子。

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');
    });
}

一切工作正常,一個標簽有很多帖子,一個標簽有很多標簽,是多對多關系。

帖子模型:

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

標簽型號:

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

我可以在帖子上顯示所有標簽,但是我想按標簽顯示相關帖子,當我說相關帖子時,我的意思是訪問者正在閱讀的“當前帖子”。 假設此當前帖子具有microsoft,google,apple,cars標簽,我希望這些標簽的相關帖子。 我不知道這是否可能或更容易按類別進行。

新聞控制器邏輯:

這是我擁有發布視圖所有邏輯的地方。

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);
}

我做了$tags3變量來測試它,但是我沒有得到想要的東西。

提前致謝

這應該工作:

$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();

$post->tags->pluck('name')行創建一個包含所有標簽名稱(屬於該帖子)的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM