Skip to content

Commit e1dc6f5

Browse files
committed
README.md updated
1 parent d9a908b commit e1dc6f5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,35 @@
55
[![Stable](https://poser.pugx.org/vkovic/laravel-custom-casts/v/stable)](https://packagist.org/packages/vkovic/laravel-custom-casts)
66
[![License](https://poser.pugx.org/vkovic/laravel-custom-casts/license)](https://packagist.org/packages/vkovic/laravel-custom-casts)
77

8-
### Make your own custom cast type for Laravel model attributes
8+
### Make your own cast type for Laravel model attributes
99

10-
Laravel custom casts works similarly to [Laravel default accessors and mutators](https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators),
11-
but with one noticeable difference: we can take our casting/mutating logic (getters, setters) away from models and put it in separate class.
10+
Laravel custom casts works similarly to [Laravel attribute casting](https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting), but with our customly defined logic (in separated class). Beside casting to
11+
our complex types this package gives us ability to listen and react to underlying model events.
1212

13-
For even more convenience our custom cast classes have ability to react to underlying model events.
13+
Let's check out some common - Laravel default cast types and possible examples of their usage:
1414

15-
>For example, this could be very useful if we're storing image with custom casts and we need to delete it
16-
>when model changes. See the [old documentation](https://github.com/vkovic/laravel-custom-casts/tree/v1.0.2#example-casting-user-image) for this examples.
15+
```php
16+
// File: app/User.php
1717

18-
### :package: :package: :package: ... Awesome new package ... :package: :package: :package:
18+
namespace App;
1919

20-
As we're near 1000 downloads of `laravel-custom-casts`, I decided to create one more handy and useful package. It's collection of some neat Laravel `artisan` commands that I'm sure most of you will find useful.
20+
use Illuminate\Database\Eloquent\Model;
2121

22-
Check it out: [vkovic/laravel-commando](https://github.com/vkovic/laravel-commando)
22+
class User extends Model
23+
{
24+
protected $casts = [
25+
'is_admin' => 'boolean',
26+
'login_count' => 'integer'
27+
'height' => 'decimal:2'
28+
];
29+
}
30+
```
31+
32+
Beside `bolean`, `integer` and `decimal` from the example above, out of the box Laravel supports `real`, `float`, `double`, `string`, `object`, `array`, `collection`, `date`, `datetime`, and `timestamp` casts.
33+
34+
Sometimes it is convenient to handle more complex types with custom logic and ability to listen and react to model events. This is where this package come in handy.
2335

24-
### :package: :package: :package: :package: :package: :package: :package: :package: :package: :package: :package: :package: :package: :package:
36+
>Handling events directly from custom casts could be very useful if we're, for e.g. storing image with custom casts and we need to delete it when the model gets deleted. *Checkout the [old documentation](https://github.com/vkovic/laravel-custom-casts/tree/v1.0.2#example-casting-user-image) for this example.*
2537
2638
---
2739

@@ -41,6 +53,8 @@ composer require vkovic/laravel-custom-casts
4153

4254
### Defining custom cast class
4355

56+
This class will be responsible for our custom casting logic.
57+
4458
```php
4559
// File: app/CustomCasts/NameCast.php
4660

@@ -77,9 +91,11 @@ when name is returned from database.
7791
### Utilizing our custom casts class
7892

7993
To enable custom casts in our models, we need to use `HasCustomCasts` trait.
80-
Beside that, we need to define which filed will use our custom cast class, following standard Laravel approach.
94+
Beside that, we need to define which filed will use our custom cast (in this case `NameCast` class), following standard Laravel approach.
8195

8296
```php
97+
// File: app/User.php
98+
8399
namespace App;
84100

85101
use App\CustomCasts\NameCast;
@@ -91,7 +107,7 @@ class User extends Model
91107
use HasCustomCasts;
92108

93109
protected $casts = [
94-
'name' => NameCast::class
110+
'name' => NameCast::class // <-- Our custom cast class from above
95111
];
96112
}
97113
```

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ services:
1212
image: composer:1.8.3
1313
volumes:
1414
- .:/app
15-
depends_on:
16-
- app
1715
command: install --ignore-platform-reqs --no-scripts

0 commit comments

Comments
 (0)