You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-21Lines changed: 35 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ collection,date, datetime and timestamp.
10
10
11
11
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.
12
12
13
-
***
13
+
---
14
14
15
15
## Compatibility
16
16
@@ -24,26 +24,35 @@ Install the package via composer:
24
24
composer require movor/laravel-custom-casts
25
25
```
26
26
27
-
## Casting User Image Example
27
+
## Example: Casting User Image
28
28
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`.
30
30
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.
33
34
34
35
```php
35
36
// File: app/User.php
36
37
38
+
use App\CustomCasts\ImageCast;
39
+
37
40
// ...
38
41
42
+
protected $fillable = [
43
+
'name', 'email', 'password',
44
+
// We need to add "image" as well:
45
+
'image'
46
+
];
47
+
39
48
protected $casts = [
40
49
'image' => ImageCast::class
41
50
];
42
51
43
52
// ...
44
53
```
45
54
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.
47
56
48
57
```php
49
58
// File: app/CustomCasts/ImageCast.php
@@ -55,16 +64,17 @@ use Illuminate\Http\UploadedFile;
55
64
56
65
class ImageCast extends CustomCastableBase
57
66
{
58
-
public function setAttribute(UploadedFile $file)
67
+
public function setAttribute($file)
59
68
{
60
-
// Define storage dir
69
+
// Define storage folder
61
70
// (relative to "storage/app" folder in Laravel project)
// 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')
93
104
]);
94
105
}
95
106
96
107
// ...
97
108
```
98
109
99
-
Ok, now we have our user created and user image stored.
110
+
Ok, now we have our user created and image stored.
100
111
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.
0 commit comments