master

laravel/framework

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

HasUuids.php

TLDR

This file contains a trait called HasUuids which provides methods for generating and handling unique identifiers (UUIDs) for models in the Laravel Eloquent package.

Methods

initializeHasUuids

This method initializes the HasUuids trait.

uniqueIds

This method returns an array of columns that should receive a unique identifier.

newUniqueId

This method generates a new UUID for the model.

resolveRouteBindingQuery

This method retrieves the model for a bound value. It checks if the value is a valid UUID and throws a ModelNotFoundException if it isn't.

getKeyType

This method returns the auto-incrementing key type. If the primary key is a unique identifier, it returns 'string'.

getIncrementing

This method returns a boolean value indicating whether the IDs are incrementing. If the primary key is a unique identifier, it returns false.

<?php

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;

trait HasUuids
{
    /**
     * Initialize the trait.
     *
     * @return void
     */
    public function initializeHasUuids()
    {
        $this->usesUniqueIds = true;
    }

    /**
     * Get the columns that should receive a unique identifier.
     *
     * @return array
     */
    public function uniqueIds()
    {
        return [$this->getKeyName()];
    }

    /**
     * Generate a new UUID for the model.
     *
     * @return string
     */
    public function newUniqueId()
    {
        return (string) Str::orderedUuid();
    }

    /**
     * Retrieve the model for a bound value.
     *
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation  $query
     * @param  mixed  $value
     * @param  string|null  $field
     * @return \Illuminate\Database\Eloquent\Relations\Relation
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function resolveRouteBindingQuery($query, $value, $field = null)
    {
        if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) {
            throw (new ModelNotFoundException)->setModel(get_class($this), $value);
        }

        if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) {
            throw (new ModelNotFoundException)->setModel(get_class($this), $value);
        }

        return parent::resolveRouteBindingQuery($query, $value, $field);
    }

    /**
     * Get the auto-incrementing key type.
     *
     * @return string
     */
    public function getKeyType()
    {
        if (in_array($this->getKeyName(), $this->uniqueIds())) {
            return 'string';
        }

        return $this->keyType;
    }

    /**
     * Get the value indicating whether the IDs are incrementing.
     *
     * @return bool
     */
    public function getIncrementing()
    {
        if (in_array($this->getKeyName(), $this->uniqueIds())) {
            return false;
        }

        return $this->incrementing;
    }
}