Download the PHP package movor/laravel-custom-casts without Composer
On this page you can find all versions of the php package movor/laravel-custom-casts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-custom-casts
Laravel Custom Casts
Make your own custom cast type for Laravel model attributes
By default, from version 5 Laravel supports attribute casting. If we define $cast
property on our model, Laravel will
help us convert defined attributes to common data types. Currently supported cast types (Laravel 5.6) are: integer
,
real
, float
, double
, string
, boolean
, object
, array
, collection
, date
, datetime
and timestamp
.
If those default cast types are not enough and you want to make your own, you'r on the right track.
:heavy_exclamation_mark::heavy_exclamation_mark::heavy_exclamation_mark: MOVING GIT REPOSITORY :heavy_exclamation_mark::heavy_exclamation_mark::heavy_exclamation_mark:
This package repo and maintenance will continue on:
:truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck: :truck:
Compatibility
The package is compatible with Laravel versions >= 5.1
Installation
Install the package via composer:
Example: Casting User Image
When saving an image, there is two things that needs to be done:
- Save image name (sometimes with path) into corresponding database field
- Save image physically on the disk
As a guidance for this example we'll use default Laravel user model found in app/User.php
.
Beside basic, predefined fields: name
, email
and password
, we also want to allow user to upload his avatar. Assume
that we already have users
table with image
field (you should create seeder for this).
To utilize custom casts, we'll need to add trait to user model, and via $casts
property link it to the cast class.
Next step is to create class that'll handle casting. It must implement setAttribute
method which will take care of
saving the image (from UploadedFile object, via form upload in this case) and generating image name with path - to be preserved in database.
Let's jump to user creation example. This will trigger our custom cast logic.
Assume that we have user controller which will handle user creation. You should create this on your own.
Code below is just a simple example and should be used as guidance only.
Visit corresponding route input basic details and attach the image. After that, we'll have our user created and image stored.
But we should also handle deleting image when user is deleted. This can be accomplished by utilizing underlying eloquent events handling. Each time eloquent event is fired, logic will look up for public method with the same name in our custom cast class.
Possible method names are:
retrieved
, creating
, created
, updating
, updated
, saving
, saved
, deleting
, deleted
, restoring
and
restored
.
This should cover basics usage of custom casts.