master

laravel/framework

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

CastsAttributes.php

TLDR

The file CastsAttributes.php defines an interface CastsAttributes in the Illuminate\Contracts\Database\Eloquent namespace. This interface provides two methods get() and set() for transforming attribute values in an Eloquent model.

Methods

get()

This method is used to transform the attribute from the underlying model values. It takes the following parameters:

  • Model $model: The underlying Eloquent model.
  • string $key: The key of the attribute.
  • mixed $value: The current value of the attribute.
  • array<string, mixed> $attributes: The array of all attributes.

The method returns TGet|null, which represents the transformed value of the attribute. If the transformation cannot be performed, it returns null.

set()

This method is used to transform the attribute to its underlying model values. It takes the following parameters:

  • Model $model: The underlying Eloquent model.
  • string $key: The key of the attribute.
  • TSet|null $value: The value to be set for the attribute.
  • array<string, mixed> $attributes: The array of all attributes.

The method returns mixed, which represents the transformed value of the attribute.

END

<?php

namespace Illuminate\Contracts\Database\Eloquent;

use Illuminate\Database\Eloquent\Model;

/**
 * @template TGet
 * @template TSet
 */
interface CastsAttributes
{
    /**
     * Transform the attribute from the underlying model values.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array<string, mixed>  $attributes
     * @return TGet|null
     */
    public function get(Model $model, string $key, mixed $value, array $attributes);

    /**
     * Transform the attribute to its underlying model values.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  TSet|null  $value
     * @param  array<string, mixed>  $attributes
     * @return mixed
     */
    public function set(Model $model, string $key, mixed $value, array $attributes);
}