where(function($query) use($conditions){ if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->orderByRaw($sort)->paginate($limit, $fields); $result->each(function($item){ $item->day = date('m月d日', strtotime($item->day)); $item->start_time = date('H:i', strtotime($item->start_time)); $item->end_time = date('H:i', strtotime($item->end_time)); }); return $this->response($result); } public function findBy(array $conditions, array $fields){ $result = DiarySleep::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->first($fields); return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function create(array $data) { try { $result = DiarySleep::create($data); return $result ? $this->response($result) : $this->error()->fail(); } catch (\Exception $e) { $this->error()->dataDoesExist('今日睡眠记录已存在!'); } } public function updateBy(array $conditions, array $data) { $result = DiarySleep::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->update($data); return $result ? $this->response($result) : $this->error()->fail(); } public function deleteBy(array $conditions) { $result = DiarySleep::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->delete(); return $result ? $this->response($result) : $this->error()->fail(); } }