Closed CrisGuzmanS closed 1 year ago
Hi @CrisGuzmanS, You can get a "real" relationships by combining this package with one of my other ones: https://github.com/staudenmeir/laravel-merged-relations
class Organization extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;
public function deepPublications()
{
return $this->mergedRelationWithModel(Publication::class, 'deep_publications');
}
public function publications()
{
return $this->belongsToMany(
Publication::class,
'organization__publication',
'organization_id',
'publication_id'
);
}
public function personPublications()
{
return $this->hasManyDeep(Publication::class, ['organization__person', Person::class, 'person__publication']);
}
}
use Staudenmeir\LaravelMergedRelations\Facades\Schema;
Schema:: createOrReplaceMergeView(
'deep_publications',
[(new Organization())->publications(), (new Organization())->personPublications()]
);
Wow 🤩. That library is what i need. Thank you so much @staudenmeir
Thank you for all of your amazing contribution 🥇
Hello! ✋ i am not sure if this is the correct place to put this problem, i want to tell you about it to know if you have an approach.
I have this 3 models:
The Organization has a many to many relation with Publication (through organizationpublication table). The Person has a many to many relation with Publication (through personpublication table). The Organization has many to many relation with Person (through organization__person table).
In my Organization model i have my relation between Organization and Publication:
I want to get deep publications of the organization (the organization publications and the person publication that belongs to the organization). This is why i implemented this method:
as you see i achieved what i want. The problem is when i want to do this:
as you can see in my deepPublications method i create a new query, i was thinking to create an scope method (scopeDeepPublications), but i am still not achieving what i want. Do you know any approach to do this?