master

laravel/framework

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

ComparesRelatedModels.php

TLDR

The file ComparesRelatedModels.php contains a trait called ComparesRelatedModels within the Illuminate\Database\Eloquent\Relations\Concerns namespace. This trait provides methods to compare models in a relationship.

Methods

is($model)

Determine if the provided model is the related instance of the relationship. It compares the keys of the parent model and the related model and checks if the related model's table and connection name match the provided model. If the relationship supports partial relations and there are more than one related models, it also checks if the provided model key exists in the relationship's query.

isNot($model)

Determine if the provided model is not the related instance of the relationship. It negates the result of the is($model) method.

getParentKey()

Abstract method to get the value of the parent model's key. It needs to be implemented by the class using the ComparesRelatedModels trait.

getRelatedKeyFrom(Model $model)

Abstract method to get the value of the model's related key. It receives a model as an argument and needs to be implemented by the class using the ComparesRelatedModels trait.

compareKeys($parentKey, $relatedKey)

Compare the parent key with the related key. If either key is empty, it returns false. If both keys are integers, it compares them as integers. Otherwise, it compares the keys as strings.

<?php

namespace Illuminate\Database\Eloquent\Relations\Concerns;

use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations;
use Illuminate\Database\Eloquent\Model;

trait ComparesRelatedModels
{
    /**
     * Determine if the model is the related instance of the relationship.
     *
     * @param  \Illuminate\Database\Eloquent\Model|null  $model
     * @return bool
     */
    public function is($model)
    {
        $match = ! is_null($model) &&
               $this->compareKeys($this->getParentKey(), $this->getRelatedKeyFrom($model)) &&
               $this->related->getTable() === $model->getTable() &&
               $this->related->getConnectionName() === $model->getConnectionName();

        if ($match && $this instanceof SupportsPartialRelations && $this->isOneOfMany()) {
            return $this->query
                        ->whereKey($model->getKey())
                        ->exists();
        }

        return $match;
    }

    /**
     * Determine if the model is not the related instance of the relationship.
     *
     * @param  \Illuminate\Database\Eloquent\Model|null  $model
     * @return bool
     */
    public function isNot($model)
    {
        return ! $this->is($model);
    }

    /**
     * Get the value of the parent model's key.
     *
     * @return mixed
     */
    abstract public function getParentKey();

    /**
     * Get the value of the model's related key.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return mixed
     */
    abstract protected function getRelatedKeyFrom(Model $model);

    /**
     * Compare the parent key with the related key.
     *
     * @param  mixed  $parentKey
     * @param  mixed  $relatedKey
     * @return bool
     */
    protected function compareKeys($parentKey, $relatedKey)
    {
        if (empty($parentKey) || empty($relatedKey)) {
            return false;
        }

        if (is_int($parentKey) || is_int($relatedKey)) {
            return (int) $parentKey === (int) $relatedKey;
        }

        return $parentKey === $relatedKey;
    }
}