1

I have model where a Job belongsToMany Categories and a Category belongsToMany jobs.

I want to save a relationship, so I have this piece of code:

    $job = \Job::find('55f089d2bf076e383a8b4585');
    $category = Category::find('55f089d2bf076e383a8b457b');
    $job->categories->add($category);
    $job->save();

If I var_dump the job I see the relations, however, if I retrieve the same job again from the database the relations are empty. Which is the correct way to save them?

Lucia
  • 4,657
  • 6
  • 43
  • 57

1 Answers1

1

I was using the save method in a wrong way, this works as a regular eloquent collection:

$job->categories()->save($category);

http://laravel.com/docs/5.0/eloquent/#inserting-related-models

Lucia
  • 4,657
  • 6
  • 43
  • 57
  • Can you please answer the question posted in the stack overflow http://stackoverflow.com/questions/41462510/hasmany-relation‌​ship-issue-in-larave‌​l-5-3-mongodb-librar‌​y-jenssegers-laravel – okconfused Jan 05 '17 at 07:08