45 | */
46 | protected $casts = [
47 | ...,
48 | 'avatar' => FileCast::class,
49 | ];
50 | }
51 | ```
52 | ## Customize The Directory
53 | ```
54 | 'avatar' => FileCast::class . ':User/avatar', # this is the default value ( the attribute key name inside the model basename )
55 | ```
56 | ## Customize The Disk
57 | ```
58 | 'avatar' => FileCast::class . ':default,s3', # here we customized the disk to s3.
59 | ```
60 | ## Customize The Driver
61 | ```
62 | 'avatar' => FileCast::class . ':default,default,' . CustomDriverService::class,
63 | ```
64 | > **_Note:_** your customer driver service must implement `Bl\LaravelUploadable\Interfaces\UploadFileInterface`
65 | and has a constructor with parameter $disk
66 | ```
67 | use Bl\LaravelUploadable\Interfaces\UploadFileInterface;
68 | use Illuminate\Http\UploadedFile;
69 |
70 | class CustomDriverService implements UploadFileInterface
71 | {
72 | protected $disk;
73 |
74 | /**
75 | * __construct
76 | *
77 | * @param \Bl\LaravelUploadable\Classes\FileArgument $disk
78 | * @return void
79 | */
80 | public function __construct($disk)
81 | {
82 | $this->disk = $disk->getValue();
83 | }
84 |
85 | /**
86 | * handle store proccess of the file.
87 | *
88 | * @param UploadedFile $file
89 | * @param string $directory
90 | * @return mixed
91 | */
92 | public function store(UploadedFile $file, string $directory): mixed
93 | {
94 | // ...
95 | }
96 |
97 | /**
98 | * handle getting the file full url path.
99 | *
100 | * @param string $path
101 | * @return string
102 | */
103 | public function get(string $path): mixed
104 | {
105 | // ...
106 | }
107 |
108 | /**
109 | * handle deleting a file.
110 | *
111 | * @param string $path
112 | * @return void
113 | */
114 | public function delete(string $path): void
115 | {
116 | // ...
117 | }
118 | }
119 | ```
120 | ## Customize The Default Path
121 | ```
122 | 'avatar' => FileCast::class . ':default,default,default,null', # customize the default value to null.
123 | 'avatar' => FileCast::class . ':default,default,default,nullable', # customize the default value to null.
124 | 'avatar' => FileCast::class . ':default,default,default,avatar.png', # customize the default value to asset('avatar.png').
125 | ```
126 | That's all! After this configuration, you can send file data from the client side with the same name of each file field of the model. The package will make the magic!
127 | ## Example
128 | In frontend you can create a form-data with field name avatar.
129 |
130 | ```
131 |
143 | ```
144 | In backend you can pass all the data to the User model.
145 | ```
146 | /**
147 | * Handle the incoming request.
148 | */
149 | public function store(UploadRequest $request)
150 | {
151 | $user = \App\Models\User::query()->create(
152 | $request->validated() // or you can use $request->all() if you don't make a validation
153 | );
154 |
155 | // this get a link of the image that uploaded.
156 | $user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
157 | }
158 | ```
159 | You can update the file manually to the User model.
160 | ```
161 | /**
162 | * Handle the incoming request.
163 | */
164 | public function store(UploadRequest $request)
165 | {
166 | $user = \App\Models\User::query()->first();
167 | $user->avatar = $request->file('avatar');
168 | $user->save();
169 |
170 | // this get a link of the image that uploaded.
171 | $user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
172 | }
173 | ```
174 | > **_Note:_** when update a field with a file the package will automatic delete the old file and put the new one.
175 | ## Delete The File
176 | You can use the FileCastRemover trait in your model and when you deleting the model instance all the related files will be deleted automatically.
177 | ```
178 |
179 | use Bl\LaravelUploadable\Traits\FileCastRemover;
180 |
181 | class User extends Model
182 | {
183 | use FileCastRemover;
184 |
185 | /**
186 | * The attributes that are mass assignable.
187 | *
188 | * @var array
189 | */
190 | protected $fillable = [
191 | 'avatar',
192 | ];
193 |
194 | /**
195 | * The attributes that should be cast.
196 | *
197 | * @var array
198 | */
199 | protected $casts = [
200 | 'avatar' => FileCast::class,
201 | ];
202 | }
203 | ```
204 | And once the model instance is deleted all it's related files will be removed.
205 | ```
206 | /**
207 | * Remove the user.
208 | */
209 | public function destroy(User $user)
210 | {
211 | $user->delete();
212 | }
213 | ```
214 | ## Apply The Events
215 | You can apply events either before or after the file upload. In addition to, you can apply that globally or for custom field.
216 | ### Global Events
217 | - For apply global events before the file upload you should define a method called `beforeFileCastUpload` in your model and it take one paramter with type `\Illuminate\Http\UploadedFile` and return the same type.
218 | - For apply global events after the file uploaded you should define a void method called `afterFileCastUpload` in your model
219 | and it take one paramter with type `\Illuminate\Http\UploadedFile`.
220 | ```
221 | use Illuminate\Http\UploadedFile;
222 |
223 | class User extends model
224 | {
225 | /**
226 | * Apply before the file cast upload event.
227 | *
228 | * @param UploadedFile $file
229 | * @return UploadedFile
230 | */
231 | public function beforeFileCastUpload(UploadedFile $file): UploadedFile
232 | {
233 | return $file;
234 | }
235 |
236 | /**
237 | * Apply after the file cast upload event.
238 | *
239 | * @param UploadedFile $file
240 | * @return void
241 | */
242 | public function afterFileCastUpload(UploadedFile $file): void
243 | {
244 | dd($file);
245 | }
246 | }
247 | ```
248 | ### Custom Events
249 | For apply custom events you should create a service that implement `Bl\LaravelUploadable\Interfaces\EventUploadInterface` and path it as a parameter.
250 | ```
251 | 'avatar' => FileCast::class . ':default,default,default,default,' . CustomUploadService::class
252 | ```
253 | ```
254 | use Bl\LaravelUploadable\Interfaces\EventUploadInterface;
255 | use Illuminate\Http\UploadedFile;
256 |
257 | class CustomUploadService implements EventUploadInterface
258 | {
259 | /**
260 | * Apply before the file cast upload event.
261 | *
262 | * @param UploadedFile $file
263 | * @return UploadedFile
264 | */
265 | public function before(UploadedFile $file): UploadedFile
266 | {
267 | return $file;
268 | }
269 |
270 | /**
271 | * Apply after the file cast upload event.
272 | *
273 | * @param UploadedFile $file
274 | * @return void
275 | */
276 | public function after(UploadedFile $file): void
277 | {
278 | dd($file);
279 | }
280 | }
281 | ```
282 | > **_Note:_** when applying global and custom events in your model the priority go to the custom event.
283 | ## Contributing
284 | Feel free to comment, open issues and send PR's. Enjoy it!!
285 | ## License
286 | The MIT License (MIT). Please see [License File](license.md) for more information.
287 |
--------------------------------------------------------------------------------