海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\UserFav;
  6. /**
  7. * 用户收藏
  8. * Class UserFavController
  9. * @package App\Http\Controllers\Api
  10. */
  11. class UserFavController extends Controller
  12. {
  13. public function index()
  14. {
  15. $list = UserFav::query()
  16. ->with([
  17. 'agentProduct:id,price,original_price',
  18. 'product:id,title,pictures',
  19. ])
  20. ->where('user_id', $this->user_id)
  21. ->select('id', 'agent_product_id', 'product_id', 'created_at')
  22. ->orderBy('id', 'DESC')
  23. ->simplePaginate(15);
  24. return $this->success($list);
  25. }
  26. public function create()
  27. {
  28. $id = (int)request()->input('id');
  29. $agent_product = AgentProduct::find($id);
  30. if (!$agent_product) {
  31. return $this->error('产品不存在或已被删除');
  32. }
  33. //如果不存在则创建
  34. UserFav::query()->firstOrCreate([
  35. 'user_id' => $this->user_id,
  36. 'agent_product_id' => $agent_product->id,
  37. 'product_id' => $agent_product->product_id,
  38. ]);
  39. return $this->success();
  40. }
  41. public function delete()
  42. {
  43. $id = (int)request()->input('id');
  44. UserFav::where('user_id', $this->user_id)->find($id)->delete();
  45. return $this->success();
  46. }
  47. }