I am using Laravel 11.
I searched a lot about this but I can’t find a good solution. Of course, I may not have used the right expressions.
I have a model called A in which I have defined:
protected $primaryKey = 'pid';
This model is related to the L model as follows:
public function speaker(): HasMany
{
return $this->hasMany(L::class, 'speaker_id', 'pid');
}
It also has a many-to-many relationship with the T model through the pivot table (account_tag).
In model T we have:
public function accounts(): BelongsToMany
{
return $this->belongsToMany(A::class, 'account_tag', 'tag_id', 'account_id', 'id', 'pid');
}
Now I want to have a relationship between T and L so that T can directly access the speaker_ids' in L that are related to A that is related to T through
account_tag’.
Apparently, I should use Has Many Through, but this code doesn’t work properly in Model T:
public function likes()
{
return $this->hasManyThrough('AppModelsL', 'AppModelsA', 'tag_id', 'pid', 'tags.id', 'accounts.pid');
}
Additional explanation:
Yes. I can use query but I need to create relationship.
devloper2024ornot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.