master

laravel/framework

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

MorphPivot.php

TLDR

The provided file, MorphPivot.php, is a class that extends the Pivot class in the Illuminate\Database\Eloquent\Relations namespace. This class handles the functionality related to the polymorphic pivot tables in the Laravel Eloquent ORM.

Methods

setKeysForSaveQuery($query)

This method is used to set the keys for a save update query. It sets the $morphType and $morphClass values as conditions in the query and then calls the parent method setKeysForSaveQuery to continue the execution.

setKeysForSelectQuery($query)

This method is used to set the keys for a select query. Similar to the setKeysForSaveQuery method, it sets the $morphType and $morphClass values as conditions in the query and then calls the parent method setKeysForSelectQuery to continue the execution.

delete()

This method overrides the delete method of the parent class. It accommodates the condition of having the $morphType and $morphClass values set in order to delete the pivot model record from the database.

getMorphType()

This method returns the $morphType value of the MorphPivot instance.

setMorphType($morphType)

This method sets the $morphType value of the MorphPivot instance.

setMorphClass($morphClass)

This method sets the $morphClass value of the MorphPivot instance.

getQueueableId()

This method returns the queueable identity for the entity. The value is generated based on the foreign key, related key, $morphType, and $morphClass.

newQueryForRestoration($ids)

This method returns a new query object to restore one or more models by their queueable IDs. The $ids parameter can be either a single ID or an array of IDs. The method handles both cases, either calling the parent method newQueryForRestoration or building the query based on the individual segments of the IDs.

newQueryForCollectionRestoration(array $ids)

This method returns a new query object to restore multiple models by their queueable IDs. It takes an array of IDs as a parameter and iterates over them to build the query using the individual segments of each ID.

##Classes No classes are defined in this file.

<?php

namespace Illuminate\Database\Eloquent\Relations;

class MorphPivot extends Pivot
{
    /**
     * The type of the polymorphic relation.
     *
     * Explicitly define this so it's not included in saved attributes.
     *
     * @var string
     */
    protected $morphType;

    /**
     * The value of the polymorphic relation.
     *
     * Explicitly define this so it's not included in saved attributes.
     *
     * @var string
     */
    protected $morphClass;

    /**
     * Set the keys for a save update query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSaveQuery($query)
    {
        $query->where($this->morphType, $this->morphClass);

        return parent::setKeysForSaveQuery($query);
    }

    /**
     * Set the keys for a select query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSelectQuery($query)
    {
        $query->where($this->morphType, $this->morphClass);

        return parent::setKeysForSelectQuery($query);
    }

    /**
     * Delete the pivot model record from the database.
     *
     * @return int
     */
    public function delete()
    {
        if (isset($this->attributes[$this->getKeyName()])) {
            return (int) parent::delete();
        }

        if ($this->fireModelEvent('deleting') === false) {
            return 0;
        }

        $query = $this->getDeleteQuery();

        $query->where($this->morphType, $this->morphClass);

        return tap($query->delete(), function () {
            $this->fireModelEvent('deleted', false);
        });
    }

    /**
     * Get the morph type for the pivot.
     *
     * @return string
     */
    public function getMorphType()
    {
        return $this->morphType;
    }

    /**
     * Set the morph type for the pivot.
     *
     * @param  string  $morphType
     * @return $this
     */
    public function setMorphType($morphType)
    {
        $this->morphType = $morphType;

        return $this;
    }

    /**
     * Set the morph class for the pivot.
     *
     * @param  string  $morphClass
     * @return \Illuminate\Database\Eloquent\Relations\MorphPivot
     */
    public function setMorphClass($morphClass)
    {
        $this->morphClass = $morphClass;

        return $this;
    }

    /**
     * Get the queueable identity for the entity.
     *
     * @return mixed
     */
    public function getQueueableId()
    {
        if (isset($this->attributes[$this->getKeyName()])) {
            return $this->getKey();
        }

        return sprintf(
            '%s:%s:%s:%s:%s:%s',
            $this->foreignKey, $this->getAttribute($this->foreignKey),
            $this->relatedKey, $this->getAttribute($this->relatedKey),
            $this->morphType, $this->morphClass
        );
    }

    /**
     * Get a new query to restore one or more models by their queueable IDs.
     *
     * @param  array|int  $ids
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function newQueryForRestoration($ids)
    {
        if (is_array($ids)) {
            return $this->newQueryForCollectionRestoration($ids);
        }

        if (! str_contains($ids, ':')) {
            return parent::newQueryForRestoration($ids);
        }

        $segments = explode(':', $ids);

        return $this->newQueryWithoutScopes()
                        ->where($segments[0], $segments[1])
                        ->where($segments[2], $segments[3])
                        ->where($segments[4], $segments[5]);
    }

    /**
     * Get a new query to restore multiple models by their queueable IDs.
     *
     * @param  array  $ids
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function newQueryForCollectionRestoration(array $ids)
    {
        $ids = array_values($ids);

        if (! str_contains($ids[0], ':')) {
            return parent::newQueryForRestoration($ids);
        }

        $query = $this->newQueryWithoutScopes();

        foreach ($ids as $id) {
            $segments = explode(':', $id);

            $query->orWhere(function ($query) use ($segments) {
                return $query->where($segments[0], $segments[1])
                             ->where($segments[2], $segments[3])
                             ->where($segments[4], $segments[5]);
            });
        }

        return $query;
    }
}