public function updated(Model $model)
 {
     if ($model->isDirty('parent_id')) {
         /**
          * 同步Original数据,是為了清除parent_id的dirty狀態
          */
         $model->syncOriginal();
         $tableName = $model->getTable();
         $oldNode = $model->getOriginal('node');
         if (0 < $model->parent_id) {
             /**
              * @var Model  $parent
              */
             $parent = $model->find($model->parent_id);
             $model->node = $parent->node . $model->id . self::SEPARATOR;
             $model->save();
             //取出原来的level
             $originalLevel = Cache::pull('node-category-original-level-' . $model->id);
             //计算新level与原来的level的差值
             $i = $model->level - $originalLevel;
             DB::table($tableName)->where('node', 'like', $oldNode . '%')->where('id', '<>', $model->id)->update(['level' => DB::raw("level + {$i}"), 'node' => DB::raw("REPLACE(`node`, '{$oldNode}', '{$model->node}')")]);
         } else {
             //修改為頂級分類
             $model->level = 1;
             $model->node = self::SEPARATOR . $model->id . self::SEPARATOR;
             $model->save();
             DB::table($tableName)->where('node', 'like', $oldNode . '%')->where('id', '<>', $model->id)->update(['level' => DB::raw("level - {$model->level}"), 'node' => DB::raw("CONCAT('{$model->node}', `id`, ',')")]);
         }
     }
 }
Beispiel #2
0
 /**
  * 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 update(EditRoleRequest $request, $id)
 {
     $role = $this->roleModel->find($id);
     $data = $request->only(['name', 'description']);
     $role->update($data);
     $this->setStatusMessage(trans('aliukevicius/laravelRbac::lang.role.messageUpdated', ['name' => $data['name']]));
     return \Redirect::to($this->getRoleUrl('index'));
 }
Beispiel #4
0
 /**
  * Find a record with ID primary key
  *
  * @param $id
  * @param array $columns
  * @return mixed
  * @throws \Exception
  */
 public function find($id, $columns = array('*'))
 {
     //$this->model->find($id, $columns);
     $element = $this->model->find($id, $columns);
     if (is_null($element)) {
         throw new \Exception('Element Not Found');
     }
     return $element;
 }
 /**
  * Find an entity by id
  *
  * @param int $id
  * @param array|string $with
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function getById($id, array $with = array())
 {
     if (!empty($with)) {
         $query = $this->make($with);
         return $query->find($id);
     } else {
         return $this->model->find($id);
     }
 }
 /**
  * @param $id
  * @param array $attribute
  */
 public function update($id, array $attribute)
 {
     $object = $this->model->find($id);
     if ($object) {
         $object->fill($attribute);
         return $object->save();
     }
     return false;
 }
Beispiel #7
0
 /**
  * Find data by id.
  *
  * @param $id
  * @param array $columns
  *
  * @return mixed
  */
 public function find($id, $columns = ['*'])
 {
     $id = $this->decrypt($id);
     $model = $this->model->find($id, $columns);
     $this->resetModel();
     return $model;
 }
Beispiel #8
0
    public static function get($id)
    {
        $supply = parent::find($id);

        if ($supply == null) {
            return null;
        }

        switch ($supply->type_id) {
            case 1:
                $instance = $supply->warehouse;
                break;
            case 2:
                $instance = $supply->vehicle;
                break;
            case 3:
                $instance = $supply->personal;
                break;
            case 4:
                $instance = $supply->building;
                break;
            case 5:
                $instance = $supply->general;
                break;
            default:
                $instance = null;
        }

        return $instance;
    }
Beispiel #9
0
 /**
  * Factory Methods
  */
 public static function find($id, $columns = array('api_keys.*'))
 {
     if (is_numeric($id)) {
         return parent::find($id, $columns);
     }
     return static::query()->where('api_key', $id)->first($columns);
 }
 /**
  * @param $id
  * @return mixed
  */
 public function find($id, array $with = array())
 {
     $this->addWithCriteria($with);
     $this->applyCriteria();
     $result = $this->model->find($id);
     $this->refresh();
     return $result;
 }
Beispiel #11
0
 /**
  * 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();
 }
 /**
  * Deletes the project using the given project id.
  * DELETE /api/v1/projects/1/delete
  *
  * @param  integer $projectId Project id to delete.
  *
  * @return array
  */
 public function delete($projectId)
 {
     $project = $this->model->find($projectId);
     if ($project === null) {
         return $this->buildFailedResponse('Could not find project with id of ' . $projectId);
     }
     $project->delete();
     return ['status' => 'success', 'message' => 'Successfully deleted project.'];
 }
Beispiel #13
0
 /**
  * Retrieve a single model using its hash value
  *
  * @param $hash
  * @return null|Model
  */
 public function retrieveByHash($hash)
 {
     $id = $this->decodeHash($hash);
     if ($id) {
         $key = $this->resourceName . '.hash.' . $hash;
         return $this->cache->remember($key, 10, function () use($id) {
             return $this->model->find($id);
         });
     }
     return null;
 }
 /**
  * @param $id
  * @param array $with
  * @return \Illuminate\Database\Eloquent\Collection|Model|null|static
  * @throws EntityNotFoundException
  */
 public function findById($id, array $with = [])
 {
     if (isset($with) && !empty($with)) {
         if (!is_array($with)) {
             throw new InvalidArgumentException();
         }
         $model = $this->model->with($with)->find($id);
     } else {
         $model = $this->model->find($id);
     }
     return $model;
 }
 /**
  * @param Entity $entity
  * @param array  $data
  * @return Entity
  */
 public function saveEntity(Entity $entity, $data = array())
 {
     if ($record = $this->model->find($entity->id())) {
         if (isset($data['updated_at'])) {
             $data['updated_at'] = date("Y-m-d H:i:s");
         }
         $record->update($data);
     } else {
         $record = $this->model->create($data);
         $entity->setId($record->id);
     }
     return $entity;
 }
Beispiel #16
0
 /**
  * 复写系统方法
  * @param  mixed  $id
  * @param  array  $columns
  * @return \Illuminate\Database\Eloquent\Model|Collection|static
  */
 public static function find($id, $columns = array('*'))
 {
     # 异常化编程
     if ($result = parent::find($id, $columns)) {
         return $result;
     }
     $modelClassName = get_called_class();
     $e = $modelClassName . 'NotFindException';
     if (!class_exists($e)) {
         eval('class ' . $e . ' extends \\FiveSay\\ModelNotFoundException {}');
     }
     throw with(new $e())->setModel($modelClassName);
 }
 /**
  * Update multiple models attributes in the storage.
  *
  * @param  array  $items
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function updateMany(array $items)
 {
     $ids = array_pluck($items, $this->model->getKeyName());
     $models = $this->model->find($ids);
     foreach ($models as $model) {
         $attributes = array_first($items, function ($i, $attributes) use($model) {
             if (!array_key_exists($model->getKeyName(), $attributes)) {
                 return false;
             }
             return $attributes[$model->getKeyName()] == $model->getKey();
         });
         $this->validation()->validate('update', array_merge($attributes, [$model->getKeyName() => $model->getKey()]));
         $model->fill($attributes);
         $model->save();
     }
     return $models;
 }
Beispiel #18
0
 /**
  * DELETE: Excluir /{ids}.
  */
 public function delete()
 {
     return DB::transaction(function () {
         // Carregar IDs
         $ids = explode(',', Route::input('ids'));
         // Criar serviço
         $service = $this->getService();
         foreach ($ids as $id) {
             // Carregar model
             $model = $this->model->find($id);
             if ($model !== null) {
                 // Executar
                 $service->delete($model, $this->request);
             }
         }
         return true;
     });
 }
 /**
  * Salva ou atualiza o registro
  * @param array $input
  * @return bool
  * @throws \Exception
  */
 public function saveOrUpdate(array $input)
 {
     if (isset($input['id'])) {
         $this->model = $this->model->find($input['id']);
         if ($this->model == null) {
             throw new \Exception('Registro não foi encontrado');
         }
     } else {
         $this->model = new $this->model();
     }
     for ($i = 0; $i < count($this->fields); $i++) {
         $dados = $this->fields[$i];
         if (isset($input[$this->fields[$i]])) {
             $this->model->{$dados} = $input[$this->fields[$i]];
         }
     }
     return $this->model->save();
 }
Beispiel #20
0
 /**
  * @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;
 }
Beispiel #21
0
 /**
  * Get single model by slug
  *
  * @param string slug
  * @return object object of model
  */
 public function byId($id)
 {
     return $this->model->find($id);
 }
Beispiel #22
0
 /**
  * @param $id
  * @param array $columns
  * @return mixed
  */
 public function find($id, $columns = array('*'))
 {
     return $this->model->find($id, $columns);
 }
Beispiel #23
0
 /**
  * Find data by id
  *
  * @param $id
  * @param array $columns
  * @return mixed
  */
 public function find($id, $columns = array('*'))
 {
     $this->applyCriteria();
     $model = $this->model->find($id, $columns);
     return $this->parserResult($model);
 }
 /**
  * Remove by id .
  *
  * @param $id
  */
 public function removeById($id)
 {
     $this->source->find($id)->delete();
 }
 /**
  * Find record by its primary key.
  *
  * @param int $id
  *
  * @return Model
  */
 public function find($id)
 {
     $this->buildQuery();
     return $this->query->find($id);
 }
 /**
  * Updates a model with the latest permissions, links, and fields
  *
  * @param \Illuminate\Database\Eloquent\Model		$model
  * @param \Frozennode\Administrator\Fields\Factory	$fieldFactory
  * @param \Frozennode\Administrator\Actions\Factory	$actionFactory
  *
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function updateModel($model, FieldFactory $fieldFactory, ActionFactory $actionFactory)
 {
     //set the data model to the active model
     $this->setDataModel($model->find($model->getKey()));
     //include the item link if one was supplied
     if ($link = $this->getModelLink()) {
         $model->setAttribute('admin_item_link', $link);
     }
     //set up the model with the edit fields new data
     $model->setAttribute('administrator_edit_fields', $fieldFactory->getEditFieldsArrays(true));
     //set up the new actions data
     $model->setAttribute('administrator_actions', $actionFactory->getActionsOptions(true));
     $model->setAttribute('administrator_action_permissions', $actionFactory->getActionPermissions(true));
     return $model;
 }
Beispiel #27
0
 /**
  * Find a model by its primary key.
  * If {@link $throwOnFind} is set, will use {@link findOrFail} internally.
  *
  * @param  mixed $id
  * @param  array $columns
  * @return Ardent|Collection
  */
 public static function find($id, $columns = array('*'))
 {
     $debug = debug_backtrace(false);
     if (static::$throwOnFind && $debug[1]['function'] != 'findOrFail') {
         return self::findOrFail($id, $columns);
     } else {
         return parent::find($id, $columns);
     }
 }
 /**
  * Find by id or alias
  *
  * @param  string $idOrAlias
  * @return Product
  */
 public static function findByIdOrAlias($idOrAlias)
 {
     $product = parent::find($idOrAlias);
     if ($product) {
         return $product;
     }
     return parent::where('alias', $idOrAlias)->first();
 }
Beispiel #29
0
 /**
  * @param int $id
  * @return Model
  */
 public function item($id)
 {
     return $this->source->find($id);
 }
Beispiel #30
0
 /**
  * @param $id
  * @param array $columns
  * @return mixed
  */
 public static function get($id, $columns = ['*'])
 {
     if (func_num_args() === 1) {
         $columns = self::fields();
     }
     return parent::find($id, $columns);
 }