【问题标题】:How can I retrieve the name of the categories to which a post belongs如何检索帖子所属类别的名称
【发布时间】:2019-12-27 22:38:32
【问题描述】:

我正在通过开发一个小型博客来学习 Laravel,以了解该框架及其提供的功能。我现在面临的问题是“如何检索帖子最初所属类别的名称”

我的帖子和类别之间的关系是多对多的。 我有三张桌子

Post
Category
CategoryPost

我的帖子模型

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

我的分类模型

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

除此之外,我还没有制作任何数据透视表模型。 我需要做吗?

我的数据透视表中的记录如下:

    id  category_id  post_id  
------  -----------  ---------
     6            1         16
     7            2         16
     8            3         16
     9            1         17
    10            3         17
    11            1         18
    12            2         18

我想显示一个帖子最初所属的当前类别的名称,以便用户可以在编辑页面中添加或删除类别。

这些是我的迁移:

张贴表

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->Integer('status_id');
            $table->string('title');
            $table->text('body');
            $table->text('media');
            $table->string('tags');
            $table->timestamps();
        });
    }

分类表:

 public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->timestamps();
    });
}

CategoryPost 表:

 public function up()
{
    Schema::create('category_post', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id');
        $table->integer('post_id');
        $table->timestamps();
    });
}

【问题讨论】:

  • 您能提供您在控制器中尝试过的内容吗?
  • 公共函数编辑(Post $post) { $post=Post::find($post->id); $categories=类别::all(); return view('admin.pages.post.edit',compact('post','categories')); }
  • 这是我目前在后期控制器的编辑方法中尝试过的

标签: php laravel eloquent blogs


【解决方案1】:

编辑迁移:

CategoryPost 表

public function up()
{
    Schema::create('category_post', function (Blueprint $table) {
        $table->increments('id');
            $table->Integer('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->Integer('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
            $table->timestamps();
    });
}

然后在你的控制器中:

public function edit(Post $post) {
 $post=Post::with('categories')->find($post->id);
 $categories=Category::all(); 

 return view('admin.pages.post.edit',compact('post','categories')); }

在你的刀片中你可以做这样的事情:

@foreach($post->categories as $category)
    {{ $category->name }}
@endforeach

这应该可以,但如果您有错误,请在此处提供。

【讨论】:

  • 现在试过了,但令我惊讶的是,它既没有显示任何错误,也没有显示任何内容。甚至 dd 也没有显示任何内容
  • 当您在控制器中dd($post) 时,您会得到关系结果吗?或者dd($post->categories)时你得到什么?
  • 我只获取 Post 表中存在的数据。它没有显示任何关系数据
  • 当我 dd($post) 我得到该帖子在数据库中的数据。当我做 dd($post->categories) 它显示 Collection {#309 ▼ #items: [] }
  • 好的。这是因为您在某个地方错过了foreign_key。您能否通过 CategoryPost 表的迁移来更新您的问题。因为您的数据透视表需要来自CategoryPost 的外键,所以可能有问题。
猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
相关资源
最近更新 更多