I have a custom plan subscription project in Laravel, I have this models:
Order: id, amount, plan_id
Plan: id, name, price
Subscription: id, plan_id, starts_at, ends_at, subscriber_type,subscriber_id
Client: id, name
Here subscriber_type is AppModelsClient and subscriber_id is the client id
Plan.php
class Plan extends Model implements Sortable
{
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class);
}
....
}
Subscription.php
class Subscription extends Model{
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class);
}
}
Client.php
class Client extends Model{
public function suscriptions(): MorphMany
{
return $this->morphMany(Subscription::class, 'subscriber', 'subscriber_type', 'subscriber_id');
}
}
When a subscription is created, an order is created too with the plan_id.
I can get all the subscriptions by a given client
$client->suscriptions()->get();
But the problem is that also along with the subscription, I need to get the first order created for that user for that plan/subscription how can I do that?