Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

opheliadesign's avatar

Update HasMany relation

Hi everyone,

I'm trying to figure out how to "move" Posts from one PostCategory to another, where each Post belongsTo PostCategory and each PostCategory hasMany Post.

For example,

$category = PostCategory::with('Posts')->find(1);
$category->posts->category->associate($newCategoryId);

This does not work. Any suggestions on the best way to accomplish this? The goal is to move posts to a new category.

0 likes
3 replies
manuelro's avatar

Let's clarify the relations first. So far, this is what we've got:

  • PostCategory - Post (many to one relation)
    • A PostCategory has zero or more Posts
    • A Post belongs to one and only one PostCategory

If this is the intended relationship between these two entities then to update the relation you should:

    // ...
    $fooCategory = PostCategory::find($categoryId);
    $myPost = Post::find($postId);
    
    $fooCategory->posts()->save($myPost);

You have to add the posts relation in you PostCategory model, like this:

    // app/models/PostCategory.php
    class PostCategory extends Eloquent {
        // ...
        public function posts () {
            return $this->hasMany('Post', 'post_category_id');
        }
    }

And your posts table should contain a post_category_id column as a foreign key representing the PostCategory id.

You have also to add the postCategory relation in you Post model, like this:

    // app/models/Post.php
    class Post extends Eloquent {
        // ...
        public function postCategory () {
            return $this->belongsTo('PostCategory', 'post_category_id');
        }
    }

When you have to update a hasMany (Category has many Posts), then you use the save() method. When you have to go the other way, you use the associate() method.

Now, when you talk about categories, the usual is that this should most of the times be a Many to Many relation:

  • PostCategory - Post (many to many relation)
    • A PostCategory has zero or more Posts
    • A Post belongs to zero or more PostCategory

But for this you'll need a relational table and some extra effort.

See more about relations at http://laravel.com/docs/4.2/eloquent#relationships

2 likes
JarekTkaczyk's avatar
Level 53

@opheliadesign First off remember about the difference:

$model->relation; // result of the relation, ie. null/model for x-1 relations or collection for x-m
$model->relation(); // relation object

Now, the latter is what you want if you are going to use relation methods (like associate, save etc). However you can't associate multiple posts like you tried, so you could do this:

foreach ($category->posts as $post) {
    $post->category()->associate($otherCategoryModel); // not id
}

However, this won't be the best you can do.

In this case instead simply call single query:

$category->posts()->update(['category_id' => $newCatId]);
7 likes

Please or to participate in this conversation.