本文整理汇总了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}");
}
}
示例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);
}
示例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;
}
示例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;
}
示例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();
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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']);
}
示例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();
}
}
示例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);
}
示例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);
}