Skip to content

Commit 9384775

Browse files
committed
README.md updated
1 parent 082114b commit 9384775

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

README.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ collection,date, datetime and timestamp.
1010

1111
If those default cast types are not enough and you want to make your own, this Laravel package is here to help you accomplish that.
1212

13-
***
13+
---
1414

1515
## Compatibility
1616

@@ -24,26 +24,35 @@ Install the package via composer:
2424
composer require movor/laravel-custom-casts
2525
```
2626

27-
## Casting User Image Example
27+
## Example: Casting User Image
2828

29-
Assume that we have user model with "image" field (varchar 255 in database).
29+
Let's use default Laravel user model found in `app/User.php`.
3030

31-
We'll need to add custom cast trait to our user model, and link it to the class that'll handle casting.
32-
So, let's edit user model file which is included in Laravel by default.
31+
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).
32+
33+
To utilize custom casts, we'll need to add trait to user model, and via `$casts` property link it to the cast class.
3334

3435
```php
3536
// File: app/User.php
3637

38+
use App\CustomCasts\ImageCast;
39+
3740
// ...
3841

42+
protected $fillable = [
43+
'name', 'email', 'password',
44+
// We need to add "image" as well:
45+
'image'
46+
];
47+
3948
protected $casts = [
4049
'image' => ImageCast::class
4150
];
4251

4352
// ...
4453
```
4554

46-
Next step is to create class that'll handle casting. It must implement "setAttribute" method which will handle transforming initial value (passed to model) and any other logic involved.
55+
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) and generating image name with path - to be preserved in db.
4756

4857
```php
4958
// File: app/CustomCasts/ImageCast.php
@@ -55,16 +64,17 @@ use Illuminate\Http\UploadedFile;
5564

5665
class ImageCast extends CustomCastableBase
5766
{
58-
public function setAttribute(UploadedFile $file)
67+
public function setAttribute($file)
5968
{
60-
// Define storage dir
69+
// Define storage folder
6170
// (relative to "storage/app" folder in Laravel project)
71+
// Don't forget to create it !!!
6272
$storageDir = 'images';
6373

6474
// Generate random image name
6575
$filename = str_random() . '.' . $file->extension();
6676

67-
// Save image
77+
// Save image to predefined folder
6878
$file->storeAs($storageDir, $filename);
6979

7080
// This will be stored in db field: "image"
@@ -74,38 +84,42 @@ class ImageCast extends CustomCastableBase
7484
```
7585

7686
Let's jump to user creation example. This will trigger our custom cast logic.
77-
We'll edit user creation method that can be found in Laravel default project.
87+
88+
Assume that we have user controller which will handle user creation.
7889

7990
```php
80-
// File: app/Http/Controllers/Auth/RegisterController.php
91+
// File: app/Http/Controllers/UserController.php
8192

8293
// ...
8394

84-
protected function create(array $data)
95+
protected function create(Request $request)
8596
{
86-
return User::create([
87-
'name' => $data['name'],
88-
'email' => $data['email'],
89-
'password' => bcrypt($data['password']),
97+
User::create([
98+
'name' => $request->name,
99+
'email' => $request->email,
100+
'password' => bcrypt($request->password),
90101
// Past the whole Illuminate\Http\UploadedFile object,
91-
// we'll handle it in our custom cast class
92-
'image' => $data['image']
102+
// we'll handle it in our ImageCast class
103+
'image' => $request->file('image')
93104
]);
94105
}
95106

96107
// ...
97108
```
98109

99-
Ok, now we have our user created and user image stored.
110+
Ok, now we have our user created and image stored.
100111

101-
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, it'll look up for public method with the same name in our custom cast class.
112+
But we should also handle deleting image when user is deleted. This can be accomplished by utilizing underlying eloquent
113+
events handling. Each time eloquent event is fired, it'll look up for public method with the same name in our custom cast class.
102114

103115
Possible method names are:
104-
retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored.
116+
`retrieved`, `creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`, `restoring`, `restored`.
105117

106118
```php
107119
// File: app/CustomCasts/ImageCast.php
108120

121+
use Storage;
122+
109123
// ...
110124

111125
public function deleted()

0 commit comments

Comments
 (0)