【问题标题】:laravel get related posts by tagslaravel 通过标签获取相关帖子
【发布时间】:2018-05-14 01:29:43
【问题描述】:

我想通过标签获取当前帖子的相关帖子,但老实说我无法获得。

我将向您展示我的表格结构。

帖子表:

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 变量来测试它,但我没有得到我想要的。

提前致谢

【问题讨论】:

  • 所以你想得到标签完全相同的帖子?
  • 这就是我想要的@devk

标签: php laravel tags laravel-query-builder


【解决方案1】:

这应该可行:

$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-&gt;tags-&gt;pluck('name') 行创建了一个包含所有标签名称(属于帖子)的数组。

【讨论】:

  • "SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列 'id' 不明确(SQL @devk
  • 我稍后编辑了我的帖子。我在whereHas 中有-&gt;where('id') 部分。它必须摆脱它。检查您是否有我编辑后的版本。
  • 有效!!谢谢@devk 太棒了。我欠你一个人
  • @DevK 抱歉,你能解释一下你的代码吗?我需要知道每一行的想法是什么
猜你喜欢
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多