public function addMedia(StoreMediaRequest $request, News $news)
{
$photo = $request->file('photo');
$news->save();
$news->clearMediaCollection('images'); //doesn't clear images collection
$file = $news->addMedia($photo)
->usingFileName(time(). $photo->getClientOriginalName())
->toCollection('images');
return [
'url' => $file->getUrl(),
'$mediaItems' => $news->getMedia('images'),
];
}
public function medialibrary(News $news)
{
$mediaItems = $news->getMedia();
return $mediaItems //returns empty array;
}
i'm trying to pull all images in images collection in order to display in a gallery mode but $news->getMedia() always return empty array...
Help!!
Does this fix your problem?
return $news->getMedia('images');
return $news->getMedia('images');
it returns empty array
Have you added media to the images collection for that $news?
Does your Media table contain any rows?
$news->clearMediaCollection('images'); //doesn't clear collection
$file = $news->addMedia($photo)
->usingFileName(time(). $photo->getClientOriginalName())
->toCollection('images');
return $news->getMedia('images');
Try this.
$news->addMedia($photo)
->usingFileName(time(). $photo->getClientOriginalName())
->toCollection('images');
$news = $news->fresh():
$media = $news->getMedia('images');
FatalThrowableError in MediaDirController.php line 22:
Call to a member function getMedia() on null
public function medialibrary(News $news)
{
$news = $news->fresh();
$media = $news->getMedia('images'); //LINE 22
return $media;
}
public function addMedia(StoreMediaRequest $request, News $news)
{
$photo = $request->file('photo');
$news->save();
$file = $news->addMedia($photo)
->usingFileName(time(). $photo->getClientOriginalName())
->toCollection('images');
return [
'url' => $file->getUrl(),
'$mediaItems' => $news->getMedia('images'),
];
}
Is the $news being passed through a valid News object?
I don't understand what you mean by being passed through a valid News object or what am i missing
Ok, I think the problems you are reporting are not caused by this package, but simply by how you've built up your controller.
To validate that the package is working correctly do this:
$news = App\Models\Owna\News::first();
$media = $news->getMedia('images');
$media should now hold the images of the first news item.
$news = News::first(); only to pull the first data
$news = News::first(); or $news = \App\Models\Owna\News::first(); // works
$media = $news->getMedia('images');
return $media;
when i try $news = News::all(); or $news = \App\Models\OwnaNews::all(); to pull all item, i get the following error
BadMethodCallException in Macroable.php line 74:
Method getMedia does not exist.
News::all() returns a Collection. getMedia only exists on models that use the HasMedia trait.
If you want to get all images for all newsItems I suggest this solution:
News::all()->map(function($news) {
return $news->getMedia('images');
})->flatten();
If you want to know more about Eloquent I highly suggest your read through the entire docs on the subject: https://laravel.com/docs/5.3/eloquent
If you want to know more about Collections read through those docs as well: https://laravel.com/docs/5.3/eloquent-collections#available-methods
$news->getMedia('images') does work, but why? Why $news->getMedia() doesn't?
i ran into the same problem
still not got solution
$market =Market::first();
$media = $market->getMedia('image');
return $media;

Most helpful comment
Does this fix your problem?