CasesController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * DateTime: 2022/10/2 13:32
  5. *
  6. * @description
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Models\Product;
  10. use App\Models\Showroom;
  11. use App\Models\StatShowroom;
  12. use Dingo\Api\Http\Request;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\JsonResponse;
  15. class CasesController extends Controller
  16. {
  17. public function search(Request $request): JsonResponse
  18. {
  19. $limit = $request->input('limit');
  20. $keywords = $request->input('keywords', '');
  21. $page = request()->input('page',1);
  22. $offset = ($page - 1) * $limit;
  23. $lists = Showroom::withCount(['viewer'])
  24. ->where('is_opened', 1)
  25. ->when($keywords, function ($query, $keywords) {
  26. /* @var Builder $query*/
  27. return $query->where('name','like', "%$keywords%");
  28. })
  29. ->orderByDesc('viewer_count')
  30. ->orderByDesc('sort')
  31. ->limit($limit)
  32. ->offset($offset)
  33. ->get();
  34. return $this->success($lists);
  35. }
  36. public function detail($id): JsonResponse
  37. {
  38. $info = Showroom::where('id',$id)
  39. ->first();
  40. return $this->success($info);
  41. }
  42. public function viewer($id): JsonResponse
  43. {
  44. $stat = new StatShowroom();
  45. $stat->showroom_id = $id;
  46. $stat->user_id = \user()->id;
  47. $stat->save();
  48. return $this->success();
  49. }
  50. }