PlanController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Services\PlanService;
  6. use Illuminate\Http\Request;
  7. use App\Models\Plan;
  8. use Illuminate\Support\Facades\DB;
  9. class PlanController extends Controller
  10. {
  11. public function fetch(Request $request)
  12. {
  13. $user = User::find($request->user['id']);
  14. if ($request->input('id')) {
  15. $plan = Plan::where('id', $request->input('id'))->first();
  16. if (!$plan) {
  17. abort(500, __('Subscription plan does not exist'));
  18. }
  19. if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
  20. abort(500, __('Subscription plan does not exist'));
  21. }
  22. return response([
  23. 'data' => $plan
  24. ]);
  25. }
  26. $counts = PlanService::countActiveUsers();
  27. $plans = Plan::where('show', 1)
  28. ->orderBy('sort', 'ASC')
  29. ->get();
  30. foreach ($plans as $k => $v) {
  31. if ($plans[$k]->capacity_limit === NULL) continue;
  32. if (!isset($counts[$plans[$k]->id])) continue;
  33. $plans[$k]->capacity_limit = $plans[$k]->capacity_limit - $counts[$plans[$k]->id]->count;
  34. }
  35. return response([
  36. 'data' => $plans
  37. ]);
  38. }
  39. }