master

laravel/framework

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

MorphMany.php

TLDR

The MorphMany.php file is a part of the Illuminate\Database\Eloquent\Relations namespace in the Demo Projects project. It contains the MorphMany class which extends the MorphOneOrMany class. The class provides methods for handling a one-to-many polymorphic relationship in Laravel's Eloquent ORM.

Methods

one

This method converts the relationship to a "morph one" relationship and returns an instance of the MorphOne class.

getResults

This method retrieves the results of the relationship. If the parent key is not null, it executes the query and returns the results. Otherwise, it returns a new collection of the related model.

initRelation

This method initializes the relation on a set of models by setting an empty collection as the value of the relation on each model.

match

This method matches the eagerly loaded results to their parent models. It calls the matchMany method from the parent class.

forceCreate

This method creates a new instance of the related model with the given attributes, allowing mass assignment. It also sets the morph type attribute to the morph class value.

forceCreateQuietly

This method creates a new instance of the related model with the given attributes, allowing mass assignment, without raising model events. It uses the withoutEvents method from the Model class to temporarily disable model events.

<?php

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class MorphMany extends MorphOneOrMany
{
    /**
     * Convert the relationship to a "morph one" relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function one()
    {
        return MorphOne::noConstraints(fn () => new MorphOne(
            $this->getQuery(),
            $this->getParent(),
            $this->morphType,
            $this->foreignKey,
            $this->localKey
        ));
    }

    /**
     * Get the results of the relationship.
     *
     * @return mixed
     */
    public function getResults()
    {
        return ! is_null($this->getParentKey())
                ? $this->query->get()
                : $this->related->newCollection();
    }

    /**
     * Initialize the relation on a set of models.
     *
     * @param  array  $models
     * @param  string  $relation
     * @return array
     */
    public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->related->newCollection());
        }

        return $models;
    }

    /**
     * Match the eagerly loaded results to their parents.
     *
     * @param  array  $models
     * @param  \Illuminate\Database\Eloquent\Collection  $results
     * @param  string  $relation
     * @return array
     */
    public function match(array $models, Collection $results, $relation)
    {
        return $this->matchMany($models, $results, $relation);
    }

    /**
     * Create a new instance of the related model. Allow mass-assignment.
     *
     * @param  array  $attributes
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function forceCreate(array $attributes = [])
    {
        $attributes[$this->getMorphType()] = $this->morphClass;

        return parent::forceCreate($attributes);
    }

    /**
     * Create a new instance of the related model with mass assignment without raising model events.
     *
     * @param  array  $attributes
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function forceCreateQuietly(array $attributes = [])
    {
        return Model::withoutEvents(fn () => $this->forceCreate($attributes));
    }
}