Laravel访问器将类别ID转换为类别标题

时间:2014-06-14 18:14:51

标签: php laravel-4 relationship

感谢您抽出宝贵时间提供帮助。

我正在建立一个可以关联类别的博客。我通过id将关联的猫保存在博客表中。

例如:category_blog_id = 1,3,9

我想通过标题检索类别,所以最好的方法是在博客模型上编写一个访问者。

有人能指出我正确的方向吗?

我应该将CategoryBlog添加到模型中,然后展开category_blog_id并运行foreach版本吗?

这就是我一直在尝试的,但它还没有正常工作,我想知道是否有更好的,更多的Laravel-y方式来做到这一点?

非常感谢。

2 个答案:

答案 0 :(得分:1)

不要在数据库中存储分隔值!而是在blog_categoryblogs表之外引入多对多(透视)表categories。它允许您使用关系数据库为您提供的方式(例如JOIN)正常维护和查询您的数据。

此类表的架构可能如下所示:

CREATE TABLE blog_category
(
  blog_id INT UNSIGNED NOT NULL,
  category_id INT UNSIGNED NOT NULL,
  PRIMARY KEY (blog_id, category_id),
  FOREIGN KEY (blog_id) REFERENCES blogs (id),
  FOREIGN KEY (category_id) REFERENCES categories (id)
);

此类表的Laravel迁移可能类似于

class CreateBlogCategoryTable extends Migration {

    public function up() {
        Schema::create('blog_category', function(Blueprint $table)
        {
            $table->integer('blog_id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->foreign('blog_id')->references('id')->on('blogs');
            $table->foreign('category_id')->references('id')->on('categories');
            $table->primary(['blog_id', 'category_id']);
        });
    }
    public function down() {
        Schema::drop('blog_category');
    }
}

Laravel Eloquent支持开箱即用的多对多relationships

Blog模型中

class Blog extends Eloquent {

    public function categories() {
        return $this->belongsToMany('Category');
    }
}

并在Category模型中

class Category extends Eloquent {

    public function blogs() {
        return $this->belongsToMany('Blog');
    }    
}

现在您可以通过类别访问博客

$blogs = Category::find(1)->blogs();

或特定博客所属的类别

$categories = Blog::find(1)->categories();

答案 1 :(得分:1)

实际上,PostCategory之间的关系可以是many-to-many,因为Category下可以有多个帖子,Post也可以不止一个Category。所以,如果是这种情况,那么你应该创建三个表,如:

表 - posts

id  | post_title  | post_slug  | post_content | Others...

表 - categories

id  | category_title  | category_slug  | Others...

表格 - category_post(数据透视表/维护帖子和类别之间的关系):

id  | category_id  | post_id

然后您需要two模型为PostCategory

// Post model
class Post extends Eloquent {
    public function categories() {
        return $this->belongsToMany('Category');
    }
}

// Category model
class Category extends Eloquent {
    public function posts() {
        return $this->belongsToMany('Post');
    }
}

使用以下内容创建类别:

category::create(array(...)); // Input::all() (Mass Assignment)

您也可以使用

创建Category
$category = new category;
$category->category_title = Input::get('category_title');
$category->category_slug = Input::get('category_slug');
// other fields (if have any)
$category->save();

现在创建Post并附加类别:

$post =  new Post; // Assume that, this is the first post so id would be 1
$post->title = 'My Post'; // Input::get('post_title');
$post->slug = 'mypost';   // Input::get('post_slug');
// Assign other values like post_content etc then
$post->save();

保存Post后:

// Attach two categories with this post using category id
$post->categories()->sync(array(1, 2)); // These (1, 2) are category ids

所以,现在你有一个Post,这个Post属于类别,换句话说,这个帖子是通过同步帖子和类别在两个类别下创建的,你实际上正在制作帖子和两个类别之间的关系以及这些关系数据将保存在category_post表中,因此根据此示例,您category_post表将包含以下内容:

id  | category_id  | post_id
----------------------------
1   |      1       |   1
2   |      2       |   1

现在您可以query使用Post模型获取所有类别的帖子:

$posts = Post::with('categories')->get();

还可以使用以下内容找到与id相关的类别的单个帖子:

$post = Post::with('categories')->find(1);

您可以使用以下方式访问相关类别:

$post->categories->get(0); // first category from the collection
$post->categories->get(1); // second category from the collection

如果您将Post模型的集合传递给您的视图,就像这样;

$posts = Post::with('categories')->get();
return View::make('post.index')->with('posts', $posts);

然后在视图中,您可以循环所有帖子和类别:

@foreach($posts as $post)
    {{ $post->post_title }}
    {{ $post->post_content }}
    @foreach($post->categories as $category)
        {{ $category->title }}
    @endforeach
@endforeach

您也可以使用Category模型,如:

// Get all categories with related posts
$categories = Category::with('posts')->get();

// Get a category using it's id with related posts
$category = Category::with('posts')->find(2); // Category id 2

这是基本想法,请阅读手册(Eloquent ORM)了解更多信息。

相关问题