laravellaravel-medialibrary

Download all files in laravel media library without collection name


I'm trying to download all the uploads together under one user. If one user has uploaded under multiple collections, I want to download all uploads under all collections in one click as a zip file.

I tried without the collection name in getMedia function. But it's not getting my result.

$user = User::where('id',auth()->user()->id)->first();
$downloads = $user->getMedia();
return MediaStream::create('my-files.zip')->addMedia($downloads);

How can I download all files without the collection name in the media library in one click as a zip file?


Solution

  • I got the solution.

    With collection name

    $user = User::where('id',auth()->user()->id)->first();
    $downloads = $user->getMedia('passport');
    return MediaStream::create('myfiles.zip')->addMedia($downloads);
    

    Without collection name

    $user = User::where('id',auth()->user()->id)->first();
    $downloads = ModelsMedia::where('model_id',$user->id)->get();
    return MediaStream::create('myfiles.zip')->addMedia($downloads);