ModelNotFoundException.php
TLDR
This file is a part of the Illuminate\Database\Eloquent namespace in the Demo Projects project. It contains the ModelNotFoundException
class, which extends the RecordsNotFoundException
class. The ModelNotFoundException
class is used to handle exceptions when a query for a specific model does not return any records.
Methods
setModel
This method is used to set the affected Eloquent model and instance IDs. It takes two parameters: $model
, which is the class name of the model, and $ids
, which is an optional parameter that represents the IDs of the affected model instances. The method sets the $model
and $ids
properties of the class, and also updates the exception message accordingly.
getModel
This method returns the class name of the affected Eloquent model.
getIds
This method returns an array containing the IDs of the affected Eloquent model instances.
<?php
namespace Illuminate\Database\Eloquent;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Support\Arr;
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
*/
class ModelNotFoundException extends RecordsNotFoundException
{
/**
* Name of the affected Eloquent model.
*
* @var class-string<TModel>
*/
protected $model;
/**
* The affected model IDs.
*
* @var array<int, int|string>
*/
protected $ids;
/**
* Set the affected Eloquent model and instance ids.
*
* @param class-string<TModel> $model
* @param array<int, int|string>|int|string $ids
* @return $this
*/
public function setModel($model, $ids = [])
{
$this->model = $model;
$this->ids = Arr::wrap($ids);
$this->message = "No query results for model [{$model}]";
if (count($this->ids) > 0) {
$this->message .= ' '.implode(', ', $this->ids);
} else {
$this->message .= '.';
}
return $this;
}
/**
* Get the affected Eloquent model.
*
* @return class-string<TModel>
*/
public function getModel()
{
return $this->model;
}
/**
* Get the affected Eloquent model IDs.
*
* @return array<int, int|string>
*/
public function getIds()
{
return $this->ids;
}
}