dongpang4470 2014-06-14 18:14
浏览 39
已采纳

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

Thanks for taking the time to help.

I am building a blog in which I can associate categories. I'm saving the associated cats in the blog table by id.

Eg: category_blog_id = 1, 3, 9

I want to retrieve the categories by there title so thought the best approach was to write an accessor on the blog model.

Could someone point me in the right direction with this?

Should I add use CategoryBlog to the model and then explode the category_blog_id and run a foreach ver it?

That is what I have been trying, but it isn't working quite right yet and I wondered if there is a better, more Laravel-y way to do this?

Many thanks.

  • 写回答

2条回答 默认 最新

  • dongwenhui8900 2014-06-14 19:14
    关注

    Actually, the relation between Post and Category could be many-to-many because a Category can has multiple posts under it and also a Post could be in more than one Category. So, if this is the case then you should create three tables like:

    Table - posts:

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

    Table - categories:

    id  | category_title  | category_slug  | Others...
    

    Table - category_post (Pivot table/ maintains relation between posts and categories):

    id  | category_id  | post_id
    

    Then you need two Models as Post and Category:

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

    Create categories using something like this:

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

    Also you may create a Category using:

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

    Now create Post and attach categories:

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

    Once the Post is saved then:

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

    So, now you have a Post and this Post belongs to to categories, in other words, this post has been created under two categories by syncing the the post and categories, you are actually making a relation between the post and two categories and these relational data will be saved in category_post table so according to this example you category_post table will contain something like this:

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

    Now you may query using Post model to get all the posts with categories like this:

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

    Also find a single post by id with categories related to it using something like this:

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

    You may access the related categories using this:

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

    If you pass the collection of Post models to your view like this;

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

    Then in the view you may loop all posts and categories like this:

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

    You may also use the Category model like:

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

    This is the basic idea, read the manual (Eloquent ORM) for more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊