Lucia
Lucia

Reputation: 4767

Save a relation with moloquent (laravel / mongodb)

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?

Upvotes: 1

Views: 913

Answers (1)

Lucia
Lucia

Reputation: 4767

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

Upvotes: 1

Related Questions