master

laravel/framework

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

LazyLoadingViolationException.php

TLDR

This file defines the LazyLoadingViolationException class, which is an exception that is thrown when an attempt is made to lazy load a relation on a model, but lazy loading is disabled.

Classes

LazyLoadingViolationException

This class extends the RuntimeException class and represents an exception that is thrown when an attempt is made to lazy load a relation on a model, but lazy loading is disabled. It has two public properties:

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

The class also defines a constructor method __construct($model, $relation) that takes an $model object and a $relation string as parameters. The constructor sets the $model and $relation properties and calls the parent constructor with an error message.

<?php

namespace Illuminate\Database;

use RuntimeException;

class LazyLoadingViolationException 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
     * @return static
     */
    public function __construct($model, $relation)
    {
        $class = get_class($model);

        parent::__construct("Attempted to lazy load [{$relation}] on model [{$class}] but lazy loading is disabled.");

        $this->model = $class;
        $this->relation = $relation;
    }
}