master

laravel/framework

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

RelationNotFoundException.php

TLDR

This file defines the RelationNotFoundException class, which is a subclass of the RuntimeException class. This exception is thrown when an undefined relationship is called on an Eloquent model.

Classes

RelationNotFoundException

The RelationNotFoundException class extends the RuntimeException class and represents an exception that is thrown when an undefined relationship is called on an Eloquent model. It has the following properties:

  • $model (public): The name of the affected Eloquent model.
  • $relation (public): The name of the relation.

It also has a static method:

make($model, $relation, $type = null)

Creates a new instance of RelationNotFoundException and sets its properties. It takes the following parameters:

  • $model: The Eloquent model object.
  • $relation: The name of the relation.
  • $type (optional): The type of the model (if provided).

The method returns the created RelationNotFoundException instance.

<?php

namespace Illuminate\Database\Eloquent;

use RuntimeException;

class RelationNotFoundException extends RuntimeException
{
    /**
     * The name of the affected Eloquent model.
     *
     * @var string
     */
    public $model;

    /**
     * The name of the relation.
     *
     * @var string
     */
    public $relation;

    /**
     * Create a new exception instance.
     *
     * @param  object  $model
     * @param  string  $relation
     * @param  string|null  $type
     * @return static
     */
    public static function make($model, $relation, $type = null)
    {
        $class = get_class($model);

        $instance = new static(
            is_null($type)
                ? "Call to undefined relationship [{$relation}] on model [{$class}]."
                : "Call to undefined relationship [{$relation}] on model [{$class}] of type [{$type}].",
        );

        $instance->model = $class;
        $instance->relation = $relation;

        return $instance;
    }
}