To sort the listings by the level of the subscription plan, you can modify the query in the Listing model to include an orderBy clause on the level column of the subscription plan. Here's an example:
public function Subscription()
{
return $this->belongsTo('App\Models\Subscription\Subscription')->with('plan');
}
public function scopeWithSubscriptionPlanLevel($query, $order = 'desc')
{
return $query->join('subscriptions', 'listings.subscription_id', '=', 'subscriptions.id')
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
->orderBy('plans.level', $order)
->select('listings.*');
}
Then, you can use this scope in your controller to retrieve the listings with the subscription plan level sorted:
$listings = Listing::withSubscriptionPlanLevel()->paginate(10);
This will return a paginated collection of listings sorted by the level of the subscription plan, with the highest level at the top. If you want to sort by the lowest level at the top, you can pass 'asc' as the second parameter to the scope:
$listings = Listing::withSubscriptionPlanLevel('asc')->paginate(10);