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
Do not update anything rather than `name` using the Media instance. If you need to deal with collections, please read the docs below.
159
+
**Note:**: Updating file name & disk will be added soon.
160
+
161
+
**Heads Up!:** Do not update anything other than `name` & `data` using the Media instance. If you need to deal with collections, please read the docs below.
159
162
160
-
WIP: Updating disk & file name will be added soon. PRs are welcome.
161
163
162
164
163
165
164
166
### Delete media
165
-
You can delete media by calling delete() method on an instance of Media.
167
+
You can delete a media by calling delete() method on an instance of Media.
166
168
167
169
```php
168
170
$media = Media::first();
169
171
$media->delete()
170
172
```
171
-
*Heads Up!* When a Media instance gets deleted, file will be removed from the filesystem, all the association with your app models & MediaCollection will be removed as well. Isn't that cool?
173
+
174
+
Or you delete media like this:
175
+
```php
176
+
Media::destroy(1);
177
+
Media::destroy([1, 2, 3]);
178
+
```
179
+
180
+
**Note:** When a Media instance gets deleted, file will be removed from the filesystem, all the association with your App Models & MediaCollection will be removed as well.
181
+
182
+
**Heads Up!:** You should not delete media using queries, e.g. `Media::where('name', 'the-file')->delete()`, this will not trigger deleted event & the file won't be deleted from the filesystem. Read more about it [here](https://laravel.com/docs/master/eloquent#deleting-models-using-queries)
172
183
173
184
174
185
-----
@@ -188,44 +199,36 @@ class Post extends Model
188
199
```
189
200
This will establish the relationship between your model and the media model.
190
201
191
-
Once done, you can associate media to the model as demonstrated below. The first parameter of the attachMedia() method can either be a media model instance, an id, a name, or an iterable list / collection of models / ids / names.
202
+
Once done, you can associate media to the model as demonstrated below.
203
+
204
+
The first parameter of the attachMedia() method can either be a media model / id or an iterable collection of models / ids.
$post->attachMedia($media); // or 1 or [1, 2, 3] or collection of media models
204
211
205
-
// Ignoring the second argument associates media in the default channel
206
-
// Include it (string) to associate in a custom channel
212
+
// Associate in a custom channel
207
213
$post->attachMedia($media, 'featured-image');
208
214
```
209
215
210
216
`attachMedia()` returns number of media attached (int) on success & null on failure.
211
217
212
218
### Disassociate media
213
-
You can use detachMedia() to disassociate media from model. It accepts only one argument & the signature of it is pretty much same as the first argument of attachMedia(), plus you can even pass null / bool / empty-string / empty-array to detach all media.
219
+
You can use detachMedia() to disassociate media from model.
$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models
224
227
225
-
// Detach all media by passing null / bool / empty-string / empty-array
226
-
$post->detachMedia([]);
228
+
// Detach all media of the default channel
229
+
$post->clearMediaChannel();
227
230
228
-
// Detach all media in a specific channel
231
+
// Detach all media of the specific channel
229
232
$post->clearMediaChannel('channel-name');
230
233
```
231
234
@@ -243,7 +246,7 @@ Apart from that, `HasMedia` trait enables your app models retrieving media conve
243
246
// All media from the default channel
244
247
$post->getMedia();
245
248
// All media from the specified channel
246
-
$post->getMedia('custom-channel');
249
+
$post->getMedia('featured-image');
247
250
```
248
251
249
252
It might be a common scenario for most of the Laravel apps to use the first media item more often, hence MediaMan has dedicated methods to retrieve the first item among all associated media.
@@ -252,12 +255,12 @@ It might be a common scenario for most of the Laravel apps to use the first medi
252
255
// First media item from the default channel
253
256
$post->getFirstMedia();
254
257
// First media item from the specified channel
255
-
$post->getFirstMedia('custom-channel');
258
+
$post->getFirstMedia('avatar');
256
259
257
260
// URL of the first media item from the default channel
258
261
$post->getFirstMediaUrl();
259
262
// URL of the first media item from the specified channel
260
-
$post->getFirstMediaUrl('custom-channel');
263
+
$post->getFirstMediaUrl('avatar');
261
264
```
262
265
263
266
@@ -268,7 +271,7 @@ MediaMan provides collections to bundle your media for better media management.
268
271
Collections are created on thy fly if it doesn't exist while uploading file.
@@ -307,39 +310,30 @@ This won't delete the media from disk but the bindings will be removed from data
307
310
308
311
------
309
312
## Media & Collections
310
-
You can create relationship between `Media` & `MediaCollection` very easily. The method signatures are similar for `Media::**Collections()` and `MediaCollection::**Media()`, which is again similar to the `HasMedia` trait.
313
+
The relationship between `Media` & `MediaCollection` are already configured. You can bind, unbind & sync binding & unbinding easily. The method signatures are similar for `Media::**Collections()` and `MediaCollection::**Media()`.
314
+
311
315
### Bind media
312
316
```php
313
317
$collection = MediaCollection::first();
314
-
// You can just pass a media model / id / name
318
+
// You can just pass a media model / id / name or an iterable collection of those
319
+
// e.g. 1 or [1, 2] or $media or [$mediaOne, $mediaTwo] or 'media-name' or ['media-name', 'another-media-name']
315
320
$collection->attachMedia($media);
316
-
317
-
// You can even pass an iterable list / collection
`attachMedia()` returns number of media attached (int) on success & null on failure. You can you`Media::attachCollections()` to bind to collections from a media model.
323
+
`attachMedia()` returns number of media attached (int) on success & null on failure. Alternatively, you can use`Media::attachCollections()` to bind to collections from a media model instance.
325
324
326
325
*Heads Up!* Unlike `HasMedia` trait, you can not have channels on media collections.
327
326
### Unbind media
328
327
```php
329
328
$collection = MediaCollection::first();
330
-
// You can just pass media model / id / name
329
+
// You can just pass a media model / id / name or an iterable collection of those
330
+
// e.g. 1 or [1, 2] or $media or [$mediaOne, $mediaTwo] or 'media-name' or ['media-name', 'another-media-name']
// Detach all media by passing null / bool / empty-string / empty-array
340
334
$collection->detachMedia([]);
341
335
```
342
-
`detachMedia()` returns number of media detached (int) on success & null on failure. You can you`Media::detachCollections()` to unbind from collections from a media model.
336
+
`detachMedia()` returns number of media detached (int) on success & null on failure. Alternatively, you can use`Media::detachCollections()` to unbind from collections from a media model instance.
343
337
344
338
### Synchronize binding & unbinding
345
339
@@ -362,4 +356,6 @@ $collection->syncMedia([]);
362
356
363
357
364
358
## Conversions
365
-
WIP: Conversions are registered globally. This means that they can be reused across your application, i.e a Post and a User can have the same sized thumbnail without having to register the same conversion twice.
359
+
Conversions are registered globally. This means that they can be reused across your application, i.e a Post and a User can have the same sized thumbnail without having to register the same conversion twice.
360
+
361
+
Note: Docs are being updated asap! Keep an eye here :)
0 commit comments