Retrieve data in pivot table many to many relationship
So I have two models Location and Company with many to many relationship, I want to get all the locations related to the company and the current_company_id of the auth user.
Here is the relationship that I'm using.
public function locations(): BelongsToMany
{
return $this->belongsToMany(Location::class)
->using(CompanyLocation::class)
->wherePivot('company_id', auth()->user()->current_company_id);
}
This is where I want to get all the locations:
Forms\Components\Select::make('location')
->label('Location')
->multiple()
->relationship('locations','location_name')
->preload()
->createOptionForm([
Forms\Components\TextInput::make('location_name')
->required(),
]),
Here is what I'm getting it seems like it is selecting the locations table and not the pivot table.
1 Reply
The relationship is in the Company model, right? Why would you
->wherePivot()
? Isn't the company ID also in ->where('id')
?
And the locations data is expected to be in the locations table and not in the pivot table, isn't it?