-
Notifications
You must be signed in to change notification settings - Fork 12k
[13.x] Add 'image' cast to model
#60949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shaedrich
wants to merge
26
commits into
laravel:13.x
Choose a base branch
from
shaedrich:image-cast
base: 13.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−0
Draft
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3aab57e
Add `'image'` cast to model
shaedrich 1d23c6e
Add tests for `'image'` model cast
shaedrich 3c2b7ad
Fix typo
shaedrich 5dfb8bf
Another one
shaedrich b369454
Add missing import
shaedrich c9270c0
Fix import FQCN
shaedrich 140613a
Use longer data type for column
shaedrich ac1a723
Improve test
shaedrich c5859be
Add missing import
shaedrich 5dbe1eb
Merge branch '13.x' into image-cast
shaedrich 29731c8
Fix root cast
shaedrich 0655be1
Fix typo
shaedrich 1a43f0d
StyleCI
shaedrich 5bf9d56
Make arguments static
shaedrich 2f32b3d
Facade fake
shaedrich 235ff58
Replicate as integration test
shaedrich 2be1a4f
StyleCI
shaedrich 2b927f1
Typo
shaedrich 1651884
Use facade instead of instance method
shaedrich 2c926c4
Fix imports
shaedrich 54a9f8f
Add image cast to primitive casts
shaedrich ad1c148
StyleCI
shaedrich 49e3dca
A binary column is better suited
shaedrich e8b625f
Fix imports
shaedrich 344ca12
Remove from other test
shaedrich 2304b94
Debug
shaedrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/Integration/Database/EloquentModelImageCastingTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ]; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this make
imagecast always failed? as there nomatchfor empty string.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 additionalcase 'image'Maybe, you have a better idea how to solve this