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()))), ]; } }