Skip to content

Commit 952fa06

Browse files
authored
Merge pull request #29 from FarhanShares/update-docs
Update docs
2 parents 6b1621d + d5faf8a commit 952fa06

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

README.md

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,34 @@ You can update a media name with an instance of Media.
152152
```php
153153
$media = Media::first();
154154
$media->name = 'New name';
155+
$media->data = ['additional_data' => 'new additional data']
155156
$media->save()
156157
```
157158

158-
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.
159162

160-
WIP: Updating disk & file name will be added soon. PRs are welcome.
161163

162164

163165

164166
### 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.
166168

167169
```php
168170
$media = Media::first();
169171
$media->delete()
170172
```
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)
172183

173184

174185
-----
@@ -188,44 +199,36 @@ class Post extends Model
188199
```
189200
This will establish the relationship between your model and the media model.
190201

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.
192205

193206
```php
194207
$post = Post::first();
195208

196-
// You can just pass media model / id / name
197-
$post->attachMedia($media);
198-
199-
// You can even pass iterable list / collection
200-
$post->attachMedia(Media::all())
201-
$post->attachMedia([1, 2, 3, 4, 5]);
202-
$post->attachMedia([$mediaSix, $mediaSeven]);
203-
$post->attachMedia(['media-name', 'another-media-name']);
209+
// Associate in the default channel
210+
$post->attachMedia($media); // or 1 or [1, 2, 3] or collection of media models
204211

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
207213
$post->attachMedia($media, 'featured-image');
208214
```
209215

210216
`attachMedia()` returns number of media attached (int) on success & null on failure.
211217

212218
### 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.
214220

215221
```php
216-
// You can just pass media model / id / name
217-
$post->detachMedia($media);
222+
// Detach all media from all channels
223+
$post->detachMedia();
218224

219-
// You can even pass iterable list / collection
220-
$post->detachMedia(Media::all())
221-
$post->detachMedia([1, 2, 3, 4, 5]);
222-
$post->detachMedia([$mediaSix, $mediaSeven]);
223-
$post->detachMedia(['media-name', 'another-media-name']);
225+
// Detach the specified media
226+
$post->detachMedia($media); // or 1 or [1, 2, 3] or collection of media models
224227

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();
227230

228-
// Detach all media in a specific channel
231+
// Detach all media of the specific channel
229232
$post->clearMediaChannel('channel-name');
230233
```
231234

@@ -243,7 +246,7 @@ Apart from that, `HasMedia` trait enables your app models retrieving media conve
243246
// All media from the default channel
244247
$post->getMedia();
245248
// All media from the specified channel
246-
$post->getMedia('custom-channel');
249+
$post->getMedia('featured-image');
247250
```
248251

249252
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
252255
// First media item from the default channel
253256
$post->getFirstMedia();
254257
// First media item from the specified channel
255-
$post->getFirstMedia('custom-channel');
258+
$post->getFirstMedia('avatar');
256259

257260
// URL of the first media item from the default channel
258261
$post->getFirstMediaUrl();
259262
// URL of the first media item from the specified channel
260-
$post->getFirstMediaUrl('custom-channel');
263+
$post->getFirstMediaUrl('avatar');
261264
```
262265

263266

@@ -268,7 +271,7 @@ MediaMan provides collections to bundle your media for better media management.
268271
Collections are created on thy fly if it doesn't exist while uploading file.
269272
```php
270273
$media = MediaUploader::source($request->file('file'))
271-
->useCollection('New Collection')
274+
->useCollection('My Collection')
272275
->upload();
273276
```
274277

@@ -291,7 +294,7 @@ MediaCollection::with('media')->findByName('My Collection');
291294
You can update a collection name. It doesn't really have any other things to update.
292295
```php
293296
$collection = MediaCollection::findByName('My Collection');
294-
$collection->name = 'New Name'
297+
$collection->name = 'Our Collection'
295298
$collection->save();
296299
```
297300

@@ -307,39 +310,30 @@ This won't delete the media from disk but the bindings will be removed from data
307310

308311
------
309312
## 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+
311315
### Bind media
312316
```php
313317
$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']
315320
$collection->attachMedia($media);
316-
317-
// You can even pass an iterable list / collection
318-
$collection->attachMedia(Media::all())
319-
$collection->attachMedia([1, 2, 3, 4, 5]);
320-
$collection->attachMedia([$mediaSix, $mediaSeven]);
321-
$collection->attachMedia(['media-name']);
322321
```
323322

324-
`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.
325324

326325
*Heads Up!* Unlike `HasMedia` trait, you can not have channels on media collections.
327326
### Unbind media
328327
```php
329328
$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']
331331
$collection->detachMedia($media);
332332

333-
// You can even pass iterable list / collection
334-
$collection->detachMedia(Media::all())
335-
$collection->detachMedia([1, 2, 3, 4, 5]);
336-
$collection->detachMedia([$mediaSix, $mediaSeven]);
337-
$collection->detachMedia(['media-name', 'another-media-name']);
338-
339333
// Detach all media by passing null / bool / empty-string / empty-array
340334
$collection->detachMedia([]);
341335
```
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.
343337

344338
### Synchronize binding & unbinding
345339

@@ -362,4 +356,6 @@ $collection->syncMedia([]);
362356

363357

364358
## 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 :)

src/Traits/HasMedia.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public function attachMedia($media, string $channel = 'default', array $conversi
9292

9393
$ids = $this->parseMediaIds($media);
9494

95-
$mediaGroup = $this->getMediaChannel($channel);
95+
$mediaChannel = $this->getMediaChannel($channel);
9696

97-
if ($mediaGroup && $mediaGroup->hasConversions()) {
97+
if ($mediaChannel && $mediaChannel->hasConversions()) {
9898
$conversions = array_merge(
9999
$conversions,
100-
$mediaGroup->getConversions()
100+
$mediaChannel->getConversions()
101101
);
102102
}
103103

@@ -114,6 +114,7 @@ public function attachMedia($media, string $channel = 'default', array $conversi
114114
});
115115
}
116116

117+
// todo: use sync($ids, false)
117118
$this->media()->attach($ids, [
118119
'channel' => $channel,
119120
]);
@@ -156,11 +157,11 @@ public function registerMediaChannels()
156157
*/
157158
protected function addMediaChannel(string $name)
158159
{
159-
$group = new MediaChannel();
160+
$channel = new MediaChannel();
160161

161-
$this->mediaChannels[$name] = $group;
162+
$this->mediaChannels[$name] = $channel;
162163

163-
return $group;
164+
return $channel;
164165
}
165166

166167
/**

tests/HasMediaTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function it_can_retrieve_all_the_media_from_the_default_channel()
131131
}
132132

133133
/** @test */
134-
public function it_can_retrieve_all_the_media_from_the_specified_group()
134+
public function it_can_retrieve_all_the_media_from_the_specified_channel()
135135
{
136136
$media = factory(Media::class, 2)->create();
137137

@@ -179,15 +179,15 @@ public function it_can_retrieve_the_first_media_from_the_specified_channel()
179179
}
180180

181181
/** @test */
182-
public function it_will_only_retrieve_media_from_the_specified_group()
182+
public function it_will_only_retrieve_media_from_the_specified_channel()
183183
{
184184
$defaultMedia = factory(Media::class)->create();
185185
$galleryMedia = factory(Media::class)->create();
186186

187-
// Attach media to the default group...
187+
// Attach media to the default channel...
188188
$this->subject->attachMedia($defaultMedia->id);
189189

190-
// Attach media to the gallery group...
190+
// Attach media to the gallery channel...
191191
$this->subject->attachMedia($galleryMedia->id, 'gallery');
192192

193193
$allDefaultMedia = $this->subject->getMedia();
@@ -291,7 +291,7 @@ public function it_can_detach_specific_media_items()
291291
}
292292

293293
/** @test */
294-
public function it_can_detach_all_the_media_in_a_specified_group()
294+
public function it_can_detach_all_the_media_in_a_specified_channel()
295295
{
296296
$mediaOne = factory(Media::class)->create();
297297
$mediaTwo = factory(Media::class)->create();

0 commit comments

Comments
 (0)