public function apply(Model $model, RepositoryContract $repository) { $fields = $this->fields; $query = $this->query; if (!$fields) { $fields = $model->getFillable(); } switch ($this->type) { case self::$ST_USE_AT_BEGIN: $query = '%' . $this->query; break; case self::$ST_USE_AT_END: $query = $this->query . '%'; break; case self::$ST_USE_AT_BOTH: $query = '%' . $this->query . '%'; break; } $query = $model->where(function ($q) use($fields, $query) { foreach ($fields as $i => $field) { if ($i == 0) { $q->where($field, 'like', $query); } $q->orWhere($field, 'like', $query); } }); return $query; }
/** * Get an Item from storage with optional relations * * @param $id * @param array $with * @return Item */ public function get($id, $with = []) { if (count($with)) { return $this->model->where('id', '=', $id)->with($with)->first(); } return $this->model->find($id); }
public function getAds() { $query = $this->model->where('active', 1)->whereHas('photos', function ($q) { $q->latest()->take(1); })->take(2)->get(); return $query; }
/** * @param string $name * @param mixed $value * @return null|object */ public function findBy($name, $value) { $model = $this->model->where($name, strtolower($value)); if ($model) { return $this->convertFormat($model->first()); } return null; }
/** * Return all object within the where conditions * Best Usage with compact('value1', 'value2') * * @param $args * @return Collection|mixed */ public function where($args) { /** * Remove if value is set to all so everything would be selected */ $args = collect($args)->filter(function ($value) { return $value != 'all' ?: false; }); return $this->model->where($args->toArray())->get(); }
/** * @param array $searchParams * @param int $limit * @param int $offset * @return mixed */ public function search($searchParams, $limit = 0, $offset = 0) { $query = $this->model->where('id', '!=', 0); foreach ($searchParams as $field => $value) { $query->where($field, '=', $value); } if ($limit > 0) { $query->take($limit)->skip($offset); } $results = $query->get(); return $this->getCollection($results); }
/** * Sets the where clause to the current entity. * * @param array|Closure $query * @param bool $append * @return self */ public function where($query, $append = false) { $entity = $this->entity; switch ($query) { case $query instanceof Closure: // Overwrite: Strip down the Builder to a Model when // the current entity has a where clause and $append is false if (!$append && $entity instanceof Builder) { $entity = $this->entity->getModel(); } // Supplied callback return to be attached try { $entity = call_user_func($query, $entity); if (!$entity instanceof Builder) { throw new InvalidCallbackReturn(); } } catch (InvalidCallbackReturn $e) { //Retain the current entity as it was before this method's call. $entity = $this->entity; } // Assign the entity with the newly attached where clause $this->entity = $entity; break; case is_array($query): foreach ($query as $clause) { $this->entity->where($clause); } break; } return $this; }
public static function retrieve_range_id($book, $chapter, $range) { $closingVerse; $verse = parent::where('Book', '=', $book)->where('Chapter', '=', $chapter)->where('Verse', '<=', $range)->orderBy('id', 'desc')->first(); $return = array('id' => $verse->id, 'chapter' => $verse->Chapter, 'verse' => $verse->Verse); return $return; }
/** * Export a column from a table. * * @param resource $fp * @param Model $entity * @param string $key_column * @param string $data_column * @param string $comment */ private function scanTable($fp, Model $entity, $key_column, $data_column, $comment) { $table = $entity->getTable(); $this->info($table . '...'); foreach ($entity->where($data_column, '<>', '')->get() as $item) { fprintf($fp, self::PO_FORMAT, 'DATABASE: ' . $key_column . '="' . $item->getAttribute($key_column) . '"', $comment, $item->getAttribute($data_column)); } }
/** * 当前属性以特定字符串结尾 * @param $attribute * @param string|null $value * @return static */ public function whereEndsWith($attribute, string $value) { if (is_empty_string($value)) { return $this; } $this->original->where($attribute, 'like', '%' . trim($value, '%')); return $this; }
/** * Find by id or alias * * @param string $idOrAlias * @return Category */ public static function findByIdOrAlias($idOrAlias) { $category = parent::find($idOrAlias); if ($category) { return $category; } return parent::where('alias', $idOrAlias)->first(); }
public function findAllByFieldPaginated($field, $value = null, $operator = '=', $perPage = null, $orderBy = null, $with = null, $columns = array('*')) { $query = $this->model->where($field, $operator, $value); $query = $this->applyAllClauses($query, $orderBy, $with); $models = $query->paginate($this->perPage($perPage), $columns); $this->resetModel(); return $this->parseResult($models); }
/** * @param array $where * @param string $boolean * @return $this */ public function where(array $where, $boolean = 'and') { foreach ($where as $k => $v) { list($field, $condition, $value) = is_array($v) ? $v : [$k, '=', $v]; $this->model = $this->model->where($field, $condition, $value, $boolean); } return $this; }
/** * @param $field * @param $value * @return mixed */ public function findAllBy($field, $value, array $with = array()) { $this->addWithCriteria($with); $this->applyCriteria(); $result = $this->model->where($field, '=', $value)->get(); $this->refresh(); return $result; }
/** * @param array $input * @param array $criteria * @return static|bool */ public function createOrFail($input, $criteria) { $object = $this->model->where($criteria)->get(); if ($object->isEmpty()) { return $this->create($input); } else { return null; } }
public static function getMenus($auth, $side = 'left') { // $menus = DB::table('menus'); // ->where('auth', '=', $auth) // ->where('side', '=', $side) // ->get(); $menus = parent::where('auth', '=', $auth)->where('side', '=', $side); return $menus; }
/** * Update user's password by e-mail. * * @author Morten Rugaard <*****@*****.**> * * @param string $email * @param string $password * @return bool */ public function updatePasswordByEmail($email, $password) { // Retrieve user by e-mail $user = $this->authModel->where('email', '=', $email)->first(); if (empty($user)) { return false; } // Update user with new password return (bool) $user->update(['password' => $password]); }
/** * Get permission Role by permission id and role id * * @return model PermissionRole */ public static function getPermissionRole($permission_id, $role_id) { $permissionRole = parent::where(['permission_id' => $permission_id, 'role_id' => $role_id])->first(); if (!empty($permissionRole)) { $permissionRole->permission_id = (int) $permissionRole->permission_id; $permissionRole->role_id = (int) $permissionRole->role_id; $permissionRole->status = (int) $permissionRole->status; } return $permissionRole; }
/** * Get permissions and roles of an route * * @param string * @return RoutePermission */ public static function getRoutePermissionsRoles($route) { $routePermission = parent::where('route', $route)->first(); if (empty($routePermission)) { return null; } $routePermission->permissions = json_decode($routePermission->permissions); $routePermission->roles = json_decode($routePermission->roles); return $routePermission; }
/** * Update e-mail verification record by e-mail. * * @author Paulius Navickas <*****@*****.**> * * @param string $email * @return bool */ public function updateVerificationByEmail($email) { // Retrieve user by e-mail $user = $this->authModel->where('email', '=', $email)->first(); if (empty($user)) { return false; } // Update user with a timestamp of when he/her was verified return (bool) $user->fill(['is_verified' => 1, 'verified_at' => Carbon::now()])->save(); }
/** * Update user's password by e-mail. * * @author Morten Rugaard <*****@*****.**> * * @param string $email * @param string $password * @return bool */ public function updatePasswordByEmail($email, $password) { // Retrieve user by e-mail $user = $this->userModel->where('email', '=', $email)->first(); if (empty($user)) { $this->errors('no-user-found-by-email', 'Could not find any user with e-mail: [' . $email . ']'); return false; } // Update user with new password return (bool) $user->update(['password' => $password]); }
/** * Applies the given where conditions to the model. * * @param array $where * @return void */ protected function applyConditions(array $where) { foreach ($where as $field => $value) { if (is_array($value)) { list($field, $condition, $val) = $value; $this->model = $this->model->where($field, $condition, $val); } else { $this->model = $this->model->where($field, '=', $value); } } }
/** * Returns a single item * * @param mixed $id * @return \Illuminate\Http\JsonResponse */ public function show($id) { try { if (is_array($id)) { return $this->response->withItem($this->model->where($id)->firstOrFail(), new $this->transformerName()); } else { return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName()); } } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } }
/** * Carregar um registro. * * @param $id * * @return mixed */ public function getModel($id) { // Carregar query $query = $this->model->where('id', $id); // Filtro de contexto $this->applyWhereContext($query); // Carregar model $ret = $query->first(); //$ret = $this->model->find($id); if (is_null($ret)) { error($this->messageModelNotFound); } $this->model = $ret; return $ret; }
/** * Find data by multiple fields * * @param array $where * @param array $columns * @return mixed */ public function findWhere(array $where, $columns = array('*')) { $this->applyCriteria(); foreach ($where as $field => $value) { if (is_array($value)) { list($field, $condition, $val) = $value; $this->model = $this->model->where($field, $condition, $val); } else { $this->model = $this->model->where($field, '=', $value); } } $model = $this->model->get($columns); $this->resetModel(); return $this->parserResult($model); }
/** * Retrieve user by "remember me" cookie. * * @author Morten Rugaard <*****@*****.**> * * @param string $recaller * @return \Nodes\Backend\Auth\Contracts\Authenticatable|null|bool */ protected function getUserByRecaller($recaller) { if (!$this->validRecaller($recaller) || $this->tokenRetrievalAttempted) { return false; } // Mark token retrieval attempt $this->tokenRetrievalAttempted = true; // Parse cookie list($id, $token) = explode('|', $recaller, 2); // Retrieve user by ID and token $user = $this->model->where('id', '=', $id)->where('remember_token', '=', $token)->first(); if (!empty($user)) { $this->viaRemember = true; } return $user; }
public static function retrieve_multiple_texts($verse_id, $range_id) { $return = ''; $texts = parent::where('id', '>=', $verse_id)->where('id', '<=', $range_id)->get(); //DB query returns two Eloquent ORM. //Limit results returned to one. // Concatenate translations together // and return. $total = count($texts) / 2; $i = 0; foreach ($texts as $text) { if ($i >= $total) { return $return; } else { $return = $return . ' ' . $text->ET; $i++; } } }
private function buildQuery() { $this->query = app(get_class($this->model)); if (!empty($this->fields)) { $this->query = $this->query->select($this->fields); } if (!empty($this->relations)) { $this->relations = array_unique($this->relations); $this->query = $this->query->with($this->relations); } if (!empty($this->per_page)) { $this->query = $this->query->take($this->per_page); } if (count($this->conditions)) { foreach ($this->conditions as $condition) { $this->query = $this->query->where($condition['column'], $condition['operator'], $condition['value'], $condition['boolean']); } } }
public function update($record, array $data, $column = 'id') { $query = $this->model->where($column, '=', $record); $numRows = $query->count(); if ($numRows === 0) { $this->result = null; return $this; } if ($numRows === 1) { $row = $query->first(); $row->update($data); $this->result = $row; return $this; } $this->result = $query->get()->each(function ($row) use($data) { $row->update($data); }); return $this; }
/** * @param int $parent_entity_id * @return int */ public function updatePositions(int $parent_entity_id = null) { // we get the entities concerned by the position incrementation regarding the given previous entity if ($parent_entity_id && ($parent_entity = $this->model->find($parent_entity_id))) { // if a parent is defined // we get the entities hierarchically inferiors to the parent $other_entities = $this->model->where('position', '>', $parent_entity->position)->orderBy('position', 'desc')->get(); } else { // if the entity has to be the master one // we get all entities $other_entities = $this->model->orderBy('position', 'desc')->get(); } // we increment the position of the selected entities foreach ($other_entities as $entities) { $entities->position += 1; $entities->save(); } // we get the new position to apply it to the current entity $new_position = isset($parent_entity) ? $parent_entity->position + 1 : 1; return $new_position; }