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 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信