當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Model::findOrFail方法代碼示例

本文整理匯總了PHP中Illuminate\Database\Eloquent\Model::findOrFail方法的典型用法代碼示例。如果您正苦於以下問題:PHP Model::findOrFail方法的具體用法?PHP Model::findOrFail怎麽用?PHP Model::findOrFail使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Illuminate\Database\Eloquent\Model的用法示例。


在下文中一共展示了Model::findOrFail方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: find

 /**
  * {@inheritdoc}
  */
 public function find($id)
 {
     try {
         return $this->model->findOrFail($id);
     } catch (ModelNotFoundException $e) {
         throw new EntityNotFoundException("Could not find entity with ID {$id}");
     }
 }
開發者ID:3ev,項目名稱:repoman,代碼行數:11,代碼來源:Repository.php

示例2: update

 /**
  * 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);
 }
開發者ID:jenky,項目名稱:laravel-api-starter,代碼行數:15,代碼來源:UserRepository.php

示例3: attach

 /**
  * 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;
 }
開發者ID:jenky,項目名稱:laravel-api-starter,代碼行數:16,代碼來源:Manager.php

示例4: update

 /**
  * 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;
 }
開發者ID:bhutanio,項目名稱:laravel-utilities,代碼行數:17,代碼來源:DataService.php

示例5: update

 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();
 }
開發者ID:cinject,項目名稱:admin-panel,代碼行數:11,代碼來源:UserController.php

示例6: update

 /**
  * 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);
 }
開發者ID:logaretm,項目名稱:depo,代碼行數:14,代碼來源:RepositoryBase.php

示例7: find

 /**
  * 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);
 }
開發者ID:sjfinder,項目名稱:repository,代碼行數:15,代碼來源:BaseRepository.php

示例8: update

 /** 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;
 }
開發者ID:sedp-mis,項目名稱:base-repository,代碼行數:15,代碼來源:BaseRepositoryEloquent.php

示例9: findOrFail

 /**
  * @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;
 }
開發者ID:ablunier,項目名稱:laravel-database,代碼行數:12,代碼來源:Repository.php

示例10: find

 /**
  * @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);
 }
開發者ID:killtw,項目名稱:repository,代碼行數:14,代碼來源:BaseRepository.php

示例11: update

 /**
  * 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;
 }
開發者ID:Jastkast,項目名稱:Page,代碼行數:18,代碼來源:BaseRepository.php

示例12: destroyModel

 /**
  * @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']);
 }
開發者ID:paybreak,項目名稱:basket,代碼行數:20,代碼來源:ModelTrait.php

示例13: show

 /**
  * 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();
     }
 }
開發者ID:ddimaria,項目名稱:Laravel-REST-CMS,代碼行數:18,代碼來源:ApiController.php

示例14: findOrFail

 /**
  * 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);
 }
開發者ID:propaganistas,項目名稱:laravel-repository,代碼行數:22,代碼來源:Repository.php

示例15: update

 /**
  * 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);
 }
開發者ID:LavaLite,項目名稱:framework,代碼行數:21,代碼來源:BaseRepository.php


注:本文中的Illuminate\Database\Eloquent\Model::findOrFail方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。