Example #1
1
 /**
  * @param $id
  * @param array $columns
  *
  * @return mixed
  */
 public function find($id, $columns = ['*'])
 {
     $this->eagerLoading();
     $this->applyCriteria();
     $results = $this->model->findOrFail($id, $columns);
     $this->resetModel();
     return $this->parseResult($results);
 }
 /**
  * Update an user.
  *
  * @param  int $id
  * @param  array $data
  * @return \Illuminate\Contracts\Auth\Authenticatable
  */
 public function update($id, array $data)
 {
     if (isset($data['password'])) {
         $data['password'] = bcrypt($data['password']);
     }
     $user = $this->model->findOrFail($id);
     return $user->update($data);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function find($id)
 {
     try {
         return $this->model->findOrFail($id);
     } catch (ModelNotFoundException $e) {
         throw new EntityNotFoundException("Could not find entity with ID {$id}");
     }
 }
Example #4
0
 /**
  * Attach the request data too the model.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int|null $id
  * @param  array $data
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function attach(Request $request, $id = null, array $data = [])
 {
     if (is_numeric($id)) {
         $this->model = $this->model->findOrFail($id);
     }
     $this->handle($request, $data);
     return $this->model;
 }
 /**
  * Update data.
  *
  * @param int $id primary key
  * @param array $data
  *
  * @return Model|bool
  */
 public function update($id, array $data)
 {
     $data = $this->format($data);
     $model = $this->model->findOrFail($id);
     if ($model->update($data)) {
         return $model;
     }
     return false;
 }
Example #6
0
 public function update(Request $request, $id)
 {
     $user = $this->userModel->findOrFail($id);
     $data = ['name' => $request->name, 'email' => $request->email];
     if ($request->has('password')) {
         $data['password'] = bcrypt($request->get('password'));
     }
     $user->update($data);
     $user->roles()->sync($request->get('roles', []));
     return redirect()->back();
 }
Example #7
0
 /**
  * Updates an existing record. $id can be a model instance.
  *
  * @param $id
  * @param array $attributes
  * @return mixed
  */
 public function update($id, array $attributes)
 {
     if ($id instanceof Model) {
         return $id->update($attributes);
     }
     return $this->model->findOrFail($id)->update($attributes);
 }
Example #8
0
 /**
  * Find data by id.
  *
  * @param $id
  * @param array $columns
  *
  * @return mixed
  */
 public function find($id, array $columns = ['*'])
 {
     $this->applyCriteria()->applyOrder();
     $model = $this->model->findOrFail($id, $columns);
     $this->makeModel();
     return $this->parseResult($model);
 }
 /** Update the model or models attributes.
  * @param  int|null  $id
  * @param  array  $attributes
  * @throws \Exception  When id is not given
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function update($id, array $attributes)
 {
     $model = $this->model->findOrFail($id);
     $this->validation()->validate('update', array_merge($attributes, [$this->model->getKeyName() => $id]));
     $model->fill($attributes);
     $model->save();
     return $model;
 }
Example #10
0
 /**
  * @param $id
  * @return mixed|Exception
  */
 public function findOrFail($id, array $with = array())
 {
     $this->addWithCriteria($with);
     $this->applyCriteria();
     $result = $this->model->findOrFail($id);
     $this->refresh();
     return $result;
 }
Example #11
0
 /**
  * Update a entity in modal by id.
  *
  * @param array $attributes
  * @param $id
  *
  * @throws ValidatorException
  *
  * @return mixed
  */
 public function update(array $attributes, $id)
 {
     $model = $this->model->findOrFail($id);
     $model->fill($attributes);
     $model->save();
     $this->resetModel();
     return $model;
 }
Example #12
0
 /**
  * @author WN
  * @param Model $model
  * @param int $id
  * @param string $modelName
  * @param string $redirect
  * @return \Illuminate\Http\RedirectResponse
  * @throws RedirectException
  */
 protected function destroyModel(Model $model, $id, $modelName, $redirect)
 {
     try {
         $model->findOrFail($id);
         $model->destroy($id);
     } catch (ModelNotFoundException $e) {
         $this->logError('Deletion of this record did not complete successfully' . $e->getMessage());
         throw (new RedirectException())->setTarget($redirect)->setError('Deletion of this record did not complete successfully');
     }
     return redirect($redirect)->with('messages', ['success' => ucwords($modelName) . ' was successfully deleted']);
 }
 /**
  * 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();
     }
 }
 /**
  * Find a model by its primary key or throw an exception.
  *
  * @param  mixed $id
  * @param  array $columns
  * @return \Illuminate\Database\Eloquent\Model
  *
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  */
 public function findOrFail($id, $columns = ['*'])
 {
     // Try-catch block to have the exception thrown from the repository instead of the model.
     try {
         $results = $this->model->findOrFail($id, $columns);
         $this->reset();
         if ($results = $this->hookFindOrFail($results)) {
             return $results;
         }
     } catch (ModelNotFoundException $e) {
     }
     throw (new ModelNotFoundException())->setModel(static::$modelClass);
 }
Example #15
0
 /**
  * Update a entity in repository by id.
  *
  * @param array $attributes
  * @param $id
  *
  * @return mixed
  */
 public function update(array $attributes, $id)
 {
     $this->applyScope();
     $_skipPresenter = $this->skipPresenter;
     $this->skipPresenter(true);
     $model = $this->model->findOrFail($id);
     $model->fill($attributes);
     $model->save();
     $this->skipPresenter($_skipPresenter);
     $this->resetModel();
     event(new RepositoryEntityUpdated($this, $model));
     return $this->parserResult($model);
 }
 /**
  * Save the form and update the existing entry in the database
  */
 public function postEdit($id, Validator $validator, Redirector $redirect, Request $request)
 {
     $rules = $this->getValidationRules('edit');
     $validator = $validator->make($request->all(), $rules);
     if ($validator->fails()) {
         return $redirect->back()->withErrors($validator->errors());
     }
     $item = $this->model->findOrFail($id);
     foreach ($rules as $field => $rules) {
         $item[$field] = $request->get($field);
     }
     if ($item->save()) {
         return $redirect->back()->withSuccess('Item has been saved.');
     }
 }
 /**
  * Update a entity in repository by id
  *
  * @throws ValidatorException
  * @param array $attributes
  * @param $id
  * @return mixed
  */
 public function update(array $attributes, $id)
 {
     if (!is_null($this->validator)) {
         $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
     }
     $_skipPresenter = $this->skipPresenter;
     $this->skipPresenter(true);
     $model = $this->model->findOrFail($id);
     $model->fill($attributes);
     $model->save();
     $this->skipPresenter($_skipPresenter);
     $this->resetModel();
     event(new RepositoryEntityUpdated($this, $model));
     return $this->parserResult($model);
 }
Example #18
0
 /**
  * Finds the first entity by the given parameters
  *
  * @param integer|array|string
  * @param string $value
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function first($params, $value = null)
 {
     switch ($params) {
         case is_numeric($params):
             $entity = $this->entity->findOrFail($params);
             break;
         case is_array($params):
             $params = !$this->hasValues($params) ? $this->getData() : $params;
             $entity = $this->entity->where($params)->firstOrFail($this->getColumns());
             break;
         case is_string($params) && !is_null($value):
             $entity = $this->entity->where($params, $value)->firstOrFail();
             break;
     }
     return $entity;
 }
 /**
  * Remove the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     // Make sure resource exists
     $resource = $this->resource->findOrFail($id);
     // Delete from storage
     if ($resource->delete()) {
         $messageType = 'success';
         $message = sprintf(_('%s successfully deleted'), $resource);
         $response = redirect()->route($this->route . '.index');
     } else {
         $messageType = 'error';
         $message = _('Unable to delete resource');
         $response = redirect()->back();
     }
     Session::flash($messageType, $message);
     return $response;
 }
Example #20
0
 public function updating(Model $model)
 {
     if ($model->isDirty('parent_id') && 0 < $model->parent_id) {
         /**
          * @var Model $parent
          */
         $parent = $model->findOrFail($model->parent_id);
         if (empty($parent)) {
             throw new \RuntimeException('Parent node is not exists.');
         }
         //檢查新的父類是否自己的子類
         if (false !== strpos($parent->node, $model->node)) {
             throw new \RuntimeException('The parent can not be your own subclass');
         }
         //把原来的level先保存在缓存中
         Cache::put('node-category-original-level-' . $model->id, $model->level, 1);
         $model->level = $parent->level + 1;
     }
 }
 /**
  * Update a entity in repository by id
  *
  * @throws ValidatorException
  *
  * @param array $attributes
  * @param       $id
  *
  * @return mixed
  */
 public function update(array $attributes, $id)
 {
     $this->applyScope();
     if (!is_null($this->validator)) {
         // we should pass data that has been casts by the model
         // to make sure data type are same because validator may need to use
         // this data to compare with data that fetch from database.
         $attributes = $this->model->newInstance()->forceFill($attributes)->toArray();
         $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
     }
     $temporarySkipPresenter = $this->skipPresenter;
     $this->skipPresenter(true);
     $model = $this->model->findOrFail($id);
     $model->fill($attributes);
     $model->save();
     $this->skipPresenter($temporarySkipPresenter);
     $this->resetModel();
     event(new RepositoryEntityUpdated($this, $model));
     return $this->parserResult($model);
 }
Example #22
0
 /**
  * Get Model by id.
  *
  * @param int $id
  * @return \App\Models\Model
  */
 public function findById($id)
 {
     return $this->model->findOrFail($id);
 }
Example #23
0
 /**
  * Find record by ID or return Exception
  * @param $id
  * @param array $columns
  * @return mixed
  */
 public function findOrFail($id, $columns = array('*'))
 {
     return $this->model->findOrFail($id, $columns);
 }
Example #24
0
 /**
  * @param $id
  * @return Model|ValidationModelInterface|ModelWithOrderFieldInterface
  */
 public function find($id)
 {
     return $this->instance->findOrFail($id);
 }
 /**
  * Find data by id
  *
  * @param       $id
  * @param array $columns
  * @return mixed|Model
  */
 public function find($id, $columns = ['*'])
 {
     return $this->wrap(function ($id, $columns = ['*']) {
         return $this->model->findOrFail($id, $columns);
     }, new Action(__METHOD__, func_get_args(), Action::READ));
 }
 /**
  * Get's an item from the repository.
  *
  * @param int $model_id
  *
  * @return \Illuminate\Database\Eloquent\Model
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  */
 public function getById($model_id)
 {
     return $this->model->findOrFail($model_id);
 }
 /**
  * {@inheritDoc}
  * @see \App\Repositories\RepositoryInterface::getById()
  */
 public function getById($id, array $columns = ['*'])
 {
     return $this->model->findOrFail($id, $columns);
 }
Example #28
0
 /**
  * Get instance by id
  *
  * @param string $id
  * @param Model $model
  * @throws ModelNotFoundException
  * @return Model|null
  */
 protected function getInstance($id, $model)
 {
     return $model->findOrFail($id);
 }
 /**
  * @author WN
  * @param Model $model
  * @param int $id
  * @param string $modelName
  * @return Model
  */
 private function fetchLocalObject(Model $model, $id, $modelName)
 {
     try {
         return $model->findOrFail($id);
     } catch (ModelNotFoundException $e) {
         $this->logError(__CLASS__ . ': Failed fetching ' . ucwords($modelName) . '[' . $id . '] local object: ' . $e->getMessage());
         throw $e;
     }
 }
Example #30
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $resource = $this->model->findOrFail($id);
     return ['success' => $resource->delete()];
 }