Home > OS >  Laravel : insert into pivot table
Laravel : insert into pivot table

Time:09-06

I'm trying to connect profiles with groups using a pivot table. What I want is to use tinker to add students profiles in study groups.

I don't know what I'm doing wrong but I can't make tinker write to the DB (I know how to do it with OneToOne and oneToMany relationships).

My Profile model has the following function :

    public function isStudentInGroup() 
{
    return $this->belongsToMany(Group::class);
}

My Group model has the following function :

    public function hasStudents() 
{
    return $this->belongsToMany(Profile::class);
}

In tinker, I start by grabbing a Profile and a group :

$profile = Profile::find(1);
$group = Group::find(1);

Then I want to add the group to the isStudentInGroup collection. How do I do it? Here's what I've tried until now. No matter what I do, everytime I use fresh() I see an empty collection.

$profile->isStudentInGroup->save($group);
$profile->isStudentInGroup->save($group->id);
$profile->isStudentInGroup->attach($group->id);
Profile::find(1)->isStudentInGroup->push(1)->update();
Profile::find(1)->isStudentInGroup->push(1)->save();

These all give BadMethodCallException errors.

A promising syntax was this one :

$profile->isStudentInGroup->push($group)
=> Illuminate\Database\Eloquent\Collection {#4646
 all: [
   App\Models\Group {#5012
     id: "1",
     start_time: "2022-09-05 18:10:00",
     end_time: "2022-09-05 19:00:00",
     created_at: null,
     updated_at: null,
     pivot: Illuminate\Database\Eloquent\Relations\Pivot {#5020
       profile_id: "1",
       group_id: "1",
     },
   },
 ],
}

But then, if I tried to $profile->save(); or $profile->update(); I would get the following

BadMethodCallException with message 'Method Illuminate\Database\Eloquent\Collection::update does not exist.'

What am I missing?

Thanks in advance.

EDIT :

Thanks to matiaslauriti I got the syntax right:

$profile->isStudentInGroup()->save($group);

I also went fishing for knowledge and found this related question with a helpful answer from McHobbes that helped me understand the logic behind the parentheses. Why eloquent model relationships do not use parenthesis and how do they work?

CodePudding user response:

Pardon me if I did not understand, but you want to make a relation between those relationships, so you have to run:

$profile->isStudentInGroup()->save($group->id);

That should trigger storing that group inside that relationship. If it does not work, I think this will should (or may also work):

$profile->isStudentInGroup()->attach($group->id);

But you must use the relationship, not the Collection. You are mixing relationships objects with Collections, that is why the error says:

Method Illuminate\Database\Eloquent\Collection::update does not exist.

A Collection does not have update or save as it is a collection, it can't be saved anywhere, but a relationship can.

CodePudding user response:

Try this

App\Models\Profile::find(1)->isStudentInGroup;

i hope it was useful .

  • Related