FavService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use App\Models\User\User;
  5. use App\Models\User\UserFav;
  6. class FavService extends BaseService
  7. {
  8. public function fav($uid, $params)
  9. {
  10. $gid = $params['id'];
  11. $fav = UserFav::where("user_id", $uid)->where("goods_id", $gid)->first();
  12. if ($fav) {
  13. $fav->delete();
  14. return $fav;
  15. }
  16. $fav = new UserFav();
  17. $fav->user_id = $uid;
  18. $fav->goods_id = $gid;
  19. $fav->save();
  20. return $fav;
  21. }
  22. public function paginate(User $user, $params)
  23. {
  24. $p = UserFav::with("good")->where("user_id", $user->id)->paginate($params['page_size']);
  25. return [
  26. "total" => $p->total(),
  27. "page_total" => $p->lastPage(),
  28. "list" => array_values(array_filter(array_map(function (UserFav $model) {
  29. if (is_null($model->good)) {
  30. return null;
  31. }
  32. return [
  33. "id" => $model->id,
  34. "name" => $model->good->name,
  35. "thumb" => $model->good->thumb,
  36. "status" => $model->good->status,
  37. ];
  38. }, $p->items()))),
  39. ];
  40. }
  41. }