where("user_id", $user->id)->firstOrFail(); } else { $address = new UserAddress(); $address->user_id = $user->id; } $address->address = $params['address']; $address->name = $params['name']; $address->phone = $params['phone']; $address->is_default = (int)$params['is_default']; if (UserAddress::where("user_id", $user->id)->count() == 0) { $address->is_default = 1; } $address->area = $params['area']; $address->save(); if ($address->is_default == 1) { UserAddress::where("user_id", $user->id)->where("id", "<>", $address->id)->update([ "is_default" => 0, ]); } } public function paginate(User $user, $params) { $p = UserAddress::where("user_id", $user->id)->orderByDesc("id")->paginate($params['page_size']); return [ "total" => $p->total(), "page_total" => $p->lastPage(), "list" => array_map(function (UserAddress $model) { return [ "id" => $model->id, "is_default" => $model->is_default, "name" => $model->name, "phone" => $model->phone, "area" => $model->area, "address" => $model->address, ]; }, $p->items()), ]; } }