query()->get(); } /** * Get Paginated * * @param $per_page * @param string $active * @param string $order_by * @param string $sort * * @return mixed */ public function getPaginated($per_page, $active = '', $order_by = 'id', $sort = 'asc') { if ($active) { return $this->query()->where('status', $active) ->orderBy($order_by, $sort) ->paginate($per_page); } else { return $this->query()->orderBy($order_by, $sort) ->paginate($per_page); } } /** * Get count of over all entries * * @return mixed */ public function getCount() { return $this->query()->count(); } /** * Find specific record by it's id * * @param $id * * @return mixed */ public function find($id) { return $this->query()->find($id); } /** * Create a blank object to pursue with further conditions * * @return mixed */ public function query() { return call_user_func(static::MODEL . '::query'); } /** * Convert all records to array format * * @param $request * @return mixed */ public function toArray($request) { return $request->toArray(); } /** * Function to check rules for request parameters. * * @param array $request * @param array $rules * * @return string[] */ protected function validate(array $request, array $rules): array { $response = [ 'status_code' => '', 'error' => '', ]; try { $validation = Validator::make($request, $rules); if ($validation->fails()) { $response['status_code'] = static::HTTP_STATUS_CODE_400; $response['error'] = $validation->messages()->first(); } } catch (\Exception $ex) { Log::error($ex); } return $response; } /** * Get latest inserted id of table * * @return int */ public function latestIdForInsertion(): int { $lastInsertedId = $this->query()->max('id'); return (!empty($lastInsertedId)) ? $lastInsertedId + 1 : 1; } }