1
- # Laravel Fileupload Component
1
+ # Laravel File Upload Component
2
+
3
+ laravel livewire file upload component
2
4
3
5
## Requirement
4
- - php 8.2
5
- - laravel 10
6
- - livewire 3
7
- - spatie/media-library
6
+ - php v8.2
7
+ - laravel v10
8
+ - livewire v3
9
+ - spatie/media-library v10
10
+
11
+ ## Installation
8
12
9
- ## Usage
13
+ ``` bash
14
+ composer install jhonoryza/laravel-fileupload-component
15
+ ```
16
+
17
+ ``` bash
18
+ php artisan vendor:publish --provider=Jhonoryza\C omponent\F ileUpload\F ileUploadServiceProvider
19
+ ```
20
+
21
+ ## Quick Start
10
22
11
23
prepare model, example Setting model
24
+
12
25
``` php
13
26
use Spatie\MediaLibrary\HasMedia;
14
27
use Spatie\MediaLibrary\InteractsWithMedia;
@@ -27,51 +40,97 @@ class Setting extends Model implements HasMedia
27
40
```
28
41
29
42
prepare livewire form class
43
+
30
44
``` php
31
- public $images = []; // property to store multiple images
45
+ /**
46
+ * property to store multiple images
47
+ */
48
+ public $images = [];
49
+
50
+ /**
51
+ * listener when there is onFileReplace event from the component
52
+ */
53
+ #[On('images:onFileReplace')]
54
+ public function replaceImages(array $images): void
55
+ {
56
+ $this->images = $images;
57
+ }
58
+
59
+ /**
60
+ * listener when there is onFileAdded event from the component
61
+ */
62
+ #[On('images:onFileAdded')]
63
+ public function addToImages(array $file): void
64
+ {
65
+ $this->images[$file['uuid']] = $file;
66
+ }
32
67
33
- // form save function example
68
+ /**
69
+ * form save function example, setting is a Model
70
+ * we call syncFileUploadRequest function
71
+ * to save images to media library
72
+ */
34
73
public function save()
35
74
{
36
- $this->setting->syncFileUploadRequest($this->images)
37
- ->toMediaCollection('settings');
75
+ $this->setting
76
+ ->syncFileUploadRequest($this->images)
77
+ ->toMediaCollection('settings');
38
78
}
39
79
```
40
80
41
- in create or edit form
81
+ in create or edit livewire component
42
82
``` php
43
83
<livewire:file-upload-component
44
84
name =" images"
45
- wire:model =" images"
46
- label =" Gambar"
85
+ label =" Image"
47
86
:model =" $setting"
48
87
collection =" settings"
49
88
:multiple =" true"
50
89
/>
51
90
```
52
91
53
- in view form
92
+ in view livewire component
54
93
``` php
55
94
<livewire:file-upload-component
56
95
name =" images"
57
- wire:model =" images"
58
- label =" Gambar"
96
+ label =" Image"
59
97
:model =" $setting"
60
98
collection =" settings"
61
99
:multiple =" true"
62
100
:canUploadFile =" false"
63
101
/>
64
102
```
65
103
104
+ ## Property Explanation
105
+ - name is required and will affect what the event name is
106
+ - : model you need to pass a variable with Model type that implement HasMedia
107
+ - collection is for media collection name
108
+ - : multiple for single file upload or multiple file upload
109
+ - : canUploadFile to hide file selector
110
+
66
111
## Component Event
67
112
68
113
this component dispatch 2 event when temporary upload is started
69
114
- media: temporary-upload-started
70
115
- media: temporary-upload-finished
71
116
72
- change media with the property name
117
+ change ` media ` with the ` name ` property, example ` name ` property is images
118
+
119
+ ``` php
120
+ <button
121
+ x-data =" { disable: false }"
122
+ x-bind:disabled =" disable"
123
+ x-bind:style =" disable ? 'cursor: not-allowed' : ''"
124
+ x-on:images:temporary-upload-started.window =" disable = true"
125
+ x-on:images:temporary-upload-finished.window =" disable = false"
126
+ type =" submit"
127
+ class =" btn btn-primary"
128
+ >
129
+ Update Setting
130
+ </button >
131
+ ```
73
132
74
- ## disable button when upload is started
133
+ or you can listen to default livewire file upload event like this
75
134
76
135
``` php
77
136
<button
@@ -89,10 +148,30 @@ change media with the property name
89
148
</button >
90
149
```
91
150
151
+ another 2 event when the file is removed / loaded or added
152
+ - media: onFileReplace
153
+ - media: onFileAdded
154
+
155
+ change ` media ` with the ` name ` property, example ` name ` property is images
156
+
157
+ ``` php
158
+ #[On('images:onFileReplace')]
159
+ public function replaceImages(array $images): void
160
+ {
161
+ $this->images = $images;
162
+ }
163
+
164
+ #[On('images:onFileAdded')]
165
+ public function addToImages(array $file): void
166
+ {
167
+ $this->images[$file['uuid']] = $file;
168
+ }
169
+ ```
170
+
92
171
## next thing todo
93
- - test validation with error message
94
- - add unit test
95
- - bug when interacts with session flash after redirect (session flash data is missing)
172
+ - [ ] test validation with error message
173
+ - [ ] add unit test
174
+ - [x] bug when interacts with session flash after redirect (session flash data is missing)
96
175
97
176
## Security
98
177
0 commit comments