master

laravel/framework

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

ClassMorphViolationException.php

TLDR

This file defines the ClassMorphViolationException class, which extends the RuntimeException class. It throws an exception when no morph map is defined for a specific Eloquent model.

Classes

ClassMorphViolationException

The ClassMorphViolationException class extends the RuntimeException class and is used to handle exceptions when no morph map is defined for a specific Eloquent model.

This class has the following properties:

  • $model: The name of the affected Eloquent model.

This class has the following method:

  • __construct($model): Constructs a new exception instance. It takes an $model object as a parameter and sets the $model property to the class name of the provided object. It also calls the parent RuntimeException constructor with a message indicating that no morph map is defined for the model.
<?php

namespace Illuminate\Database;

use RuntimeException;

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

    /**
     * Create a new exception instance.
     *
     * @param  object  $model
     */
    public function __construct($model)
    {
        $class = get_class($model);

        parent::__construct("No morph map defined for model [{$class}].");

        $this->model = $class;
    }
}