0

I try to save many to many relations inside loop here is the code

                $user = User::firstOrCreate(
                [
                    'username' => $member->username,
                    'email' => $member->email
                ],
                [
                    'password' => $member->password,
                    'name' => $member->fullname,
                    'email' => $member->email,
                    'firstname' => $member->firstname,
                    'lastname' => $member->lastname,
                    'username' => $member->username,
                    'active' => $member->active
                ]
            );


            if ($user) {

                $user->account()->firstOrCreate([
                    'user_id' => $user->id,
                    ],
                    [
                        'address' => $member->address,
                        'phone' => $member->phone,
                        'web' => $member->web,
                        'company' => $member->company,
                        'company_email' => $member->company_email,
                        'company_phone' => $member->company_phone,
                        'fax' => $member->fax,
                        'company_fax' => $member->company_fax,
                        'has_newsletter' => $member->newsletter,
                        'published' => $member->listing,
                        'zip' => $member->zip,
                        'association_id' => $member->association_id,
                        'affiliate_id' => $member->affiliate_id,

                    ]);

                if ($member->activity_group_ids) {
                    $user->account()->occupations()->snyc(explode(',',$member->activity_group_ids));
                }

                $user->assignRole('member');
            }

but I keep getting Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::occupations()

my models:

User

/**
 * @return HasOne
 */
public function account()
{
    return $this->hasOne(Member::class);
}

Member

/**
 * @return BelongsTo
 */
public function user()
{
    return $this->belongsTo(User::class);
}

/**
 * @return BelongsToMany
 */
public function occupations()
{
    return $this->belongsToMany(OccupationGroup::class, 'members_occupational_groups', 'member_id', 'occupation_group_id');
}

OccupationGroup

/**
 * @return BelongsToMany
 */
public function members()
{
    return $this->belongsToMany(Member::class, 'members_occupational_groups', 'occupation_group_id', 'member_id');
}

What is wrong in this case?

fefe
  • 8,755
  • 27
  • 104
  • 180
  • 1
    `$user->account()->occupations()` should be `$user->account->occupations()`. When you call `->account()`, it's a new Query, based on the `public function account() { // hasOne }` relationship. `$user->account` will be the actual record, which you can chain to `->occupations()->sync()`. That's the key difference between `->relationship` (no `()`) and `->relationship()`. – Tim Lewis Apr 08 '22 at 15:02
  • Thanks! works. Strange, previously I was trying the same but somehow was not working. But now I get `Method Illuminate\Database\Eloquent\Collection::snyc does not exist.` – fefe Apr 08 '22 at 15:06
  • Are you calling `->occupations->sync()` or `->occupations()->sync()`? It should be the second one. `->occupations` will return a Collection, but `->occupations()` will be the `belongsToMany()` method, which does have the `->sync()` option. Again, please learn the difference between calling something with and without the `()`; it makes a huge difference in Laravel. – Tim Lewis Apr 08 '22 at 15:27
  • 1
    Thanks for the clearings. Now works as expected – fefe Apr 09 '22 at 14:32

0 Answers0