Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\LazyLoadingViolationException;
use Illuminate\Image\Image;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
Expand All @@ -38,6 +39,7 @@
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Image as ImageFacade;
use Illuminate\Support\Str;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -123,6 +125,7 @@ trait HasAttributes
'encrypted:object',
'float',
'hashed',
'image',
'immutable_date',
'immutable_datetime',
'immutable_custom_datetime',
Expand Down Expand Up @@ -897,6 +900,12 @@ protected function castAttribute($key, $value)
return $this->asDateTime($value)->toImmutable();
case 'timestamp':
return $this->asTimestamp($value);
case 'image':
case 'image:url':
case 'image:storage':
case 'image:base64':
case 'image:bytes':
return $this->toImage($key, $value, substr($castType, 6));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this make image cast always failed? as there no match for empty string.

@shaedrich shaedrich Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. I was just trying to figure out how everything prefixed with image: would not accidentally interpreted as class cast—which it did without the additional case 'image'

Maybe, you have a better idea how to solve this

}

if ($this->isEnumCastable($key)) {
Expand Down Expand Up @@ -1640,6 +1649,17 @@ protected function asTimestamp($value)
return $this->asDateTime($value)->getTimestamp();
}

protected function toImage(string $key, $value, string $sourceType): Image
{
return match ($sourceType) {
'storage' => ImageFacade::fromStorage($value),
'bytes' => ImageFacade::fromBytes($value),
'base64' => ImageFacade::fromBase64($value),
'url' => ImageFacade::fromUrl($value),
default => throw new InvalidCastException($this->getModel(), $key, $sourceType === '' ? 'image' : 'image:'.$sourceType),
};
}

/**
* Prepare a date for array / JSON serialization.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/Integration/Database/EloquentModelImageCastingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentModelDateCastingTest;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\UploadedFile;
use Illuminate\Image\Image as ImageFacade;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

class EloquentModelImageCastingTest extends DatabaseTestCase
{
/** {@inheritdoc} */
protected function afterRefreshingDatabase()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('storage_path');
$table->string('web_url');
//$table->binary('image');
$table->text('encoded');
$table->timestamps();
});
}

public function testImageCast()
{
$file = UploadedFile::fake()->image('test.jpg', 100, 100);
$image = new ImageFacade(file_get_contents($file->getRealPath()));
$base64 = $image->toBase64();
$image->store('avatars/john_doe.png');
$img = Image::create([
'web_url' => 'https://example.com/favicon.ico',
'storage_path' => 'avatars/john_doe.png',
//'image' => $image->toBytes(),
'encoded' => $image->toBase64(),
]);

$this->assertInstanceOf(ImageFacade::class, $img->web_url);
$this->assertInstanceOf(ImageFacade::class, $img->storage_path);
//$this->assertInstanceOf(ImageFacade::class, $img->image);
$this->assertInstanceOf(ImageFacade::class, $img->encoded);
$this->assertSame($base64, $img->storage_path->toBase64());
//$this->assertSame($base64, $img->image->toBase64());
$this->assertSame($base64, $img->encoded->toBase64());
}
}

class Image extends Model
{
protected $guarded = [];

protected $casts = [
'web_url' => 'image:url',
'storage_path' => 'image:storage',
'image' => 'image:bytes',
'encoded' => 'image:base64',
];
}
Loading