JsonEncodingException.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use RuntimeException;
  4. class JsonEncodingException extends RuntimeException
  5. {
  6. /**
  7. * Create a new JSON encoding exception for the model.
  8. *
  9. * @param mixed $model
  10. * @param string $message
  11. * @return static
  12. */
  13. public static function forModel($model, $message)
  14. {
  15. return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
  16. }
  17. /**
  18. * Create a new JSON encoding exception for the resource.
  19. *
  20. * @param \Illuminate\Http\Resources\Json\JsonResource $resource
  21. * @param string $message
  22. * @return static
  23. */
  24. public static function forResource($resource, $message)
  25. {
  26. $model = $resource->resource;
  27. return new static('Error encoding resource ['.get_class($resource).'] with model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
  28. }
  29. /**
  30. * Create a new JSON encoding exception for an attribute.
  31. *
  32. * @param mixed $model
  33. * @param mixed $key
  34. * @param string $message
  35. * @return static
  36. */
  37. public static function forAttribute($model, $key, $message)
  38. {
  39. $class = get_class($model);
  40. return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}.");
  41. }
  42. }