Closed boryn closed 3 years ago
For now, I came up with this solution so that the used
key appears in the array with feature details:
$features = $user->subscription('main')->features()->get()->keyBy('id')->toArray();
$usage = $user->subscription('main')->usage()->get()->keyBy('plan_subscription_feature_id')->toArray();
$featuresWithUsage = array_replace_recursive($features, $usage);
and in $featuresWithUsage
there is something like:
[1] => Array
(
[id] => 1
[plan_subscription_id] => 1
[plan_feature_id] => 19
[tag] => posts_per_social_profile
[name] => Scheduled posts per profile
[description] =>
[value] => 30
[resettable_period] => 1
[resettable_interval] => month
[sort_order] => 10
[created_at] => 2021-05-26T09:46:01.000000Z
[updated_at] => 2021-05-26T09:46:01.000000Z
[plan_subscription_feature_id] => 1
[used] => 2 // -> here, the 'used' key
[valid_until] => 2021-06-26T09:43:01.000000Z
[laravel_through_key] => 1
)
[2] => Array
(
[id] => 2
[plan_subscription_id] => 1
[plan_feature_id] => 20
[tag] => social-profile
[name] => Social Profiles
[description] =>
[value] => 3
[sort_order] => 20
[created_at] => 2021-05-26T09:43:01.000000Z
[updated_at] => 2021-05-26T09:43:01.000000Z
)
I think you are overthinking the relationship :stuck_out_tongue: , it's as easy as:
$user->subscription('main')->features()->with('usage')->get()
You'd get (this is from my tests, yours will be different, obviously):
array:4 [
0 => array:13 [
"id" => 3
"plan_subscription_id" => "1"
"plan_feature_id" => "3"
"tag" => "analytics"
"name" => "Analytics"
"description" => null
"value" => "0"
"resettable_period" => 0
"resettable_interval" => "month"
"sort_order" => 15
"created_at" => "2021-05-26T16:00:01.000000Z"
"updated_at" => "2021-05-26T16:00:01.000000Z"
"usage" => null
]
1 => array:13 [
"id" => 2
"plan_subscription_id" => "1"
"plan_feature_id" => "2"
"tag" => "posts_per_social_profile"
"name" => "Scheduled posts per profile"
"description" => null
"value" => "30"
"resettable_period" => 1
"resettable_interval" => "month"
"sort_order" => 10
"created_at" => "2021-05-26T16:00:01.000000Z"
"updated_at" => "2021-05-26T16:00:01.000000Z"
"usage" => array:6 [
"id" => 1
"plan_subscription_feature_id" => "2"
"used" => 30
"valid_until" => "2021-06-26T16:00:01.000000Z"
"created_at" => "2021-05-26T16:00:02.000000Z"
"updated_at" => "2021-05-26T16:00:02.000000Z"
]
]
I only find the 'usage()' relationship at PlanSubscription.php
and not at PlanSubscriptionFeature.php
. Now I tried your solution with $user->subscription('main')->features()->with('usage')->get();
and I get: Illuminate\Database\Eloquent\RelationNotFoundException. Call to undefined relationship [usage] on model [Bpuig\Subby\Models\PlanSubscriptionFeature]
I still had 4.0.0-beta.2 :D After update to 4.0.0 there is this relationship :D
Is there any method for getting the subscriber's available features along with their usage? It could be useful to conveniently display this information in the user's profile.
I can see that at this moment we can run:
$features = $user->subscription('main')->features()->get();
and
$usage = $user->subscription('main')->usage()->get();
and later combine both collections?