https://github.com/movor/laravel-custom-casts
Make your own cast type for Laravel model
https://github.com/movor/laravel-custom-casts
accessor attribute cast casting custom laravel mutator
Last synced: 4 months ago
JSON representation
Make your own cast type for Laravel model
- Host: GitHub
- URL: https://github.com/movor/laravel-custom-casts
- Owner: movor
- License: mit
- Created: 2018-08-08T18:48:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-05T15:18:44.000Z (over 7 years ago)
- Last Synced: 2026-01-30T23:00:03.963Z (4 months ago)
- Topics: accessor, attribute, cast, casting, custom, laravel, mutator
- Language: PHP
- Homepage:
- Size: 19.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Custom Casts
[](https://travis-ci.org/movor/laravel-custom-casts)
[](https://packagist.org/packages/movor/laravel-custom-casts)
[](https://packagist.org/packages/movor/laravel-custom-casts)
[](https://packagist.org/packages/movor/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**:
[**vkovic/laravel-custom-casts**](https://github.com/vkovic/laravel-custom-casts).
: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:
```bash
composer require movor/laravel-custom-casts
```
## Example: Casting User Image
When saving an image, there is two things that needs to be done:
1. Save image name (sometimes with path) into corresponding database field
2. 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.
```php
// File: app/User.php
namespace App;
use App\CustomCasts\ImageCast;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Movor\LaravelCustomCasts\HasCustomCasts;
class User extends Authenticatable
{
use Notifiable, HasCustomCasts;
protected $fillable = [
'name', 'email', 'password', 'image'
];
protected $hidden = [
'password', 'remember_token'
];
protected $casts = [
'image' => ImageCast::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.
```php
// File: app/CustomCasts/ImageCast.php
namespace App\CustomCasts;
use Movor\LaravelCustomCasts\CustomCastBase;
use Illuminate\Http\UploadedFile;
class ImageCast extends CustomCastBase
{
public function setAttribute($file)
{
// Define storage folder
// (relative to "storage/app" folder in Laravel project)
// Don't forget to create it !!!
$storageDir = 'images';
// Generate random image name
$filename = str_random() . '.' . $file->extension();
// Save image to predefined folder
$file->storeAs($storageDir, $filename);
// This will be stored in db field: "image"
return $storageDir . '/' . $filename;
}
}
```
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.
```php
// File: app/Http/Controllers/UserController.php
// ...
protected function create(Request $request)
{
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
// Past the whole Illuminate\Http\UploadedFile object,
// we'll handle it in our ImageCast class
'image' => $request->file('image')
]);
}
// ...
```
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`.
```php
// File: app/CustomCasts/ImageCast.php
// Add at the top
use Storage;
// ...
// This method will be triggered after model has been deleted
public function deleted()
{
// We can access underlying model with $this->model
// and attribute name that is being casted with $this->attribute
// Retrieve image path and delete it from the disk
$imagePath = $this->model->image;
Storage::delete($imagePath);
}
// ...
```
This should cover basics usage of custom casts.