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
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,
9
-
collection,date, datetime and timestamp.
8
+
By default, from version 5 Laravel supports attribute casting. If we define cast property on our model, Laravel will
9
+
help us convert defined attributes to common data types. Currently supported cast types (Laravel 5.6) are: `integer`,
Let's use default Laravel user model found in `app/User.php`.
30
+
When saving an image, there is two things that needs to be done:
31
+
1. Save image name (sometimes with path) into corresponding database field
32
+
2. Save image physically on the disk
30
33
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).
34
+
As a guidance for this example we'll use default Laravel user model found in `app/User.php`.
35
+
36
+
Beside basic, predefined fields: `name`, `email` and `password`, we also want to allow user to upload his avatar. Assume
37
+
that we already have `users` table with `image` field (you should create seeder for this).
32
38
33
39
To utilize custom casts, we'll need to add trait to user model, and via `$casts` property link it to the cast class.
34
40
35
41
```php
36
42
// File: app/User.php
37
43
44
+
namespace App;
45
+
38
46
use App\CustomCasts\ImageCast;
47
+
use Illuminate\Foundation\Auth\User as Authenticatable;
48
+
use Illuminate\Notifications\Notifiable;
49
+
use Movor\LaravelCustomCasts\HasCustomCasts;
39
50
40
-
// ...
51
+
class User extends Authenticatable
52
+
{
53
+
use Notifiable, HasCustomCasts;
41
54
42
-
protected $fillable = [
43
-
'name', 'email', 'password',
44
-
// We need to add "image" as well:
45
-
'image'
46
-
];
55
+
protected $fillable = [
56
+
'name', 'email', 'password', 'image'
57
+
];
47
58
48
-
protected $casts = [
49
-
'image' => ImageCast::class
50
-
];
59
+
protected $hidden = [
60
+
'password', 'remember_token'
61
+
];
62
+
63
+
protected $casts = [
64
+
'image' => ImageCast::class
65
+
];
66
+
}
51
67
52
68
// ...
53
69
```
54
70
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.
71
+
Next step is to create class that'll handle casting. It must implement `setAttribute` method which will take care of
72
+
saving the image (from UploadedFile object) and generating image name with path - to be preserved in database.
56
73
57
74
```php
58
75
// File: app/CustomCasts/ImageCast.php
59
76
60
77
namespace App\CustomCasts;
61
78
62
-
use Movor\LaravelCustomCasts\CustomCastableBase;
79
+
use Movor\LaravelCustomCasts\CustomCastBase;
63
80
use Illuminate\Http\UploadedFile;
64
81
65
-
class ImageCast extends CustomCastableBase
82
+
class ImageCast extends CustomCastBase
66
83
{
67
84
public function setAttribute($file)
68
85
{
@@ -85,7 +102,10 @@ class ImageCast extends CustomCastableBase
85
102
86
103
Let's jump to user creation example. This will trigger our custom cast logic.
87
104
88
-
Assume that we have user controller which will handle user creation.
105
+
Assume that we have user controller which will handle user creation. You should create this on your
106
+
own.
107
+
108
+
> Code below is just a simple example and should be used as guidance only.
89
109
90
110
```php
91
111
// File: app/Http/Controllers/UserController.php
@@ -110,14 +130,17 @@ protected function create(Request $request)
110
130
Ok, now we have our user created and image stored.
111
131
112
132
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.
133
+
events handling. Each time eloquent event is fired, logic will look up for public method with the same name in our custom
0 commit comments