where('user_id', $user->id) ->where('status', WooStore::STATUS_ACTIVE) ->orderBy('site_name') ->orderBy('site_url') ->get(); } public function resolve(?User $user): ?WooStore { if (! $user) { return null; } $stores = $this->activeStores($user); if ($stores->isEmpty()) { return null; } $selectedId = (int) session(self::SESSION_KEY, 0); $store = $stores->firstWhere('id', $selectedId); if ($store) { return $store; } $first = $stores->first(); session([self::SESSION_KEY => $first->id]); return $first; } public function switch(User $user, int $storeId): WooStore { $store = WooStore::query() ->where('user_id', $user->id) ->whereKey($storeId) ->where('status', WooStore::STATUS_ACTIVE) ->firstOrFail(); session([self::SESSION_KEY => $store->id]); return $store; } }