While querying polymorphic relationship existence or absence, Laravel Eloquent provides whereHasMorph
and whereDoesntHaveMorph
methods which really helpful to create queries related to the types. But what about for eager loads?
Here is the situation:
Think about 3 models App\Photos, App\Videos and App\Blogs.
These three models are all feedable which means they have a polymorphic relationship with App\Feed model.
But while App\Photos and App\Videos models are reactable (have a polymorphic relationship with App\Reactions model), App\Blogs model doesn't have this relationship.
Let's check out the methods:
App\Feed
public function feedable() {
return $this->morphTo();
}
App\Reactions
public function reactable() {
return $this->morphTo();
}
App\Photos
public function feed() {
return $this->morphOne('App\Feed', 'feedable');
}
public function reactions() {
return $this->morphMany('App\Reactions', 'reactable');
}
App\Videos
public function feed() {
return $this->morphOne('App\Feed', 'feedable');
}
public function reactions() {
return $this->morphMany('App\Reactions', 'reactable');
}
App\Blogs
public function feed() {
return $this->morphOne('App\Feed', 'feedable');
}
While querying feeds from App\Feed model, I also want to get reactions count. But because of that App\Blogs model doesn't have a method named reactions
, this query causes errors.
How can I create query like this:
$feeds = Feed::with([
'feedable' => function($query) {
// I need to get the feedable_type of the feedable here.
// module_exists is a custom helper to check if an module is installed and active
if ($type != 'App\Blogs' && module_exists('Reactions')) {
$query->withCount('reactions');
}
}
])->all();
Edit
I have tried to simplify my question but I think I need to provide more information about the application.
The application is a content management system (social network system) that third party developers can develop modules for. Blogs, Photos, Videos, Feed, Reactions, Comments, Share etc. are all different modules and they can be disabled or removed by the site owners. So while developers are creating modules, they have to take into consideration that Reactions, Coments etc modules can be disabled or removed.