海南旅游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.

47 lines
1.1 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()->with([
  16. 'agentProduct' => fn($query) => $query->select('id', 'price', 'original_price'),
  17. 'product' => fn($query) => $query->select('id', 'title', 'pictures'),
  18. ])
  19. ->where('user_id', $this->user_id)
  20. ->select('id', 'agent_product_id', 'product_id', 'created_at')
  21. ->orderBy('id', 'DESC')
  22. ->simplePaginate(15);
  23. return $this->success($list);
  24. }
  25. public function create()
  26. {
  27. $id = (int)request()->input('id');
  28. $agent_product = AgentProduct::where('user_id', $this->user_id)->find($id);
  29. if (!$agent_product) {
  30. return $this->error('产品不存在或已被删除');
  31. }
  32. //如果不存在则创建
  33. UserFav::query()->firstOrCreate([
  34. 'user_id' => $this->user_id,
  35. 'agent_product_id' => $agent_product->id,
  36. 'product_id' => $agent_product->product_id,
  37. ]);
  38. return $this->success();
  39. }
  40. }