master

laravel/framework

Last updated at: 29/12/2023 09:22

JsonEncodingException.php

TLDR

This file defines the JsonEncodingException class in the Illuminate\Database\Eloquent namespace. It provides methods to create a new JSON encoding exception for a model, a resource, or an attribute.

Methods

forModel($model, $message)

Create a new JSON encoding exception for the model.

  • Parameters:
    • $model (mixed) - The model object.
    • $message (string) - The error message.
  • Returns: A new JsonEncodingException instance.

forResource($resource, $message)

Create a new JSON encoding exception for the resource.

  • Parameters:
    • $resource (\Illuminate\Http\Resources\Json\JsonResource) - The resource object.
    • $message (string) - The error message.
  • Returns: A new JsonEncodingException instance.

forAttribute($model, $key, $message)

Create a new JSON encoding exception for an attribute.

  • Parameters:
    • $model (mixed) - The model object.
    • $key (mixed) - The attribute key.
    • $message (string) - The error message.
  • Returns: A new JsonEncodingException instance.
<?php

namespace Illuminate\Database\Eloquent;

use RuntimeException;

class JsonEncodingException extends RuntimeException
{
    /**
     * Create a new JSON encoding exception for the model.
     *
     * @param  mixed  $model
     * @param  string  $message
     * @return static
     */
    public static function forModel($model, $message)
    {
        return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
    }

    /**
     * Create a new JSON encoding exception for the resource.
     *
     * @param  \Illuminate\Http\Resources\Json\JsonResource  $resource
     * @param  string  $message
     * @return static
     */
    public static function forResource($resource, $message)
    {
        $model = $resource->resource;

        return new static('Error encoding resource ['.get_class($resource).'] with model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
    }

    /**
     * Create a new JSON encoding exception for an attribute.
     *
     * @param  mixed  $model
     * @param  mixed  $key
     * @param  string  $message
     * @return static
     */
    public static function forAttribute($model, $key, $message)
    {
        $class = get_class($model);

        return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}.");
    }
}