12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace App\Modules\Mini\Services;
- use App\Base\BaseService;
- use App\Models\User\User;
- use App\Models\User\UserFav;
- class FavService extends BaseService
- {
- public function fav($uid, $params)
- {
- $gid = $params['id'];
- $fav = UserFav::where("user_id", $uid)->where("goods_id", $gid)->first();
- if ($fav) {
- $fav->delete();
- return $fav;
- }
- $fav = new UserFav();
- $fav->user_id = $uid;
- $fav->goods_id = $gid;
- $fav->save();
- return $fav;
- }
- public function paginate(User $user, $params)
- {
- $p = UserFav::with("good")->where("user_id", $user->id)->paginate($params['page_size']);
- return [
- "total" => $p->total(),
- "page_total" => $p->lastPage(),
- "list" => array_values(array_filter(array_map(function (UserFav $model) {
- if (is_null($model->good)) {
- return null;
- }
- return [
- "id" => $model->id,
- "name" => $model->good->name,
- "thumb" => $model->good->thumb,
- "status" => $model->good->status,
- ];
- }, $p->items()))),
- ];
- }
- }
|