1 week ago

Join several levels

Posted 1 week ago by avictory

Hello, I have Users that are assigned to a plans that has can_upload_photos feature with yes or no value. I need to have one Eloquent query to get collection of users with this info:

Output: User id, User name, plan_id, can_upload_photos order by can_upload_photos

Users: (User Model)
Plans: (Plan Model)
-id (key)
PlanFeatures: (PlanFeature Model)
-id (key)
-plan_id (FK to Plans.id)
-feature_slug (such as "can_upload_photos")
-value (suach as yes/no)
PlanSubscription (PlanSubscription Model):
-id (key)
-plan_id (FK to Plans.id)
-subscribeable_type & subscribeable_id (for many to many morph, such as App\Model\User & user_id) 
PlanSubscription Model: 
public function subscriptionable()    { return $this->morphTo(); }
User Model:
public function subscriptions(): MorphMany { return $this->morphMany(PlanSubscription::class, 'subscriptionable'); }
Plan model:
public function features(): HasMany { return $this->hasMany(PlanFeature::class); }
public function subscriptions(): HasMany { return $this->hasMany(PlanSubscription::class); }

Please sign in or create an account to participate in this conversation.