0

My query return results from database as comma separated values: 1,2,3..etc Then I'm trying to make a button for download and should select 1 or more documents to download ( based on if is 1 id or multiple ).

So button with one file looks like this

<a href="users/files/download/2?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>

and button where the query return multiple id's looks like this ( notice the 2,3 before the token )

<a href="users/files/download/2,3?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>

This I add to the routes.php

Route::get('/users/files/download/{fileId}', 'UsersController@getDownload');

And this to the controller

public function getDownload($fileId)
{
    $file = Documents::findOrFail($fileId);
    $file = public_path(). "/uploads/" . $file->document_path;
    return Response::download($file, 'filename.pdf');
}

Currently no matter which button I click I've got

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [Documents].

What is this means? The model is there. This is the Documents model

class Documents extends Eloquent
{
    protected $table = 'documents';
    protected $primaryKey = 'id';
    public $timestamps = false;
}

And how I can select all documents ID's when they are multiple?

Update: current code

$file = Documents::findOrFail([$fileId]);

$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name,  ZipArchive::CREATE);
    foreach ($file as $files) {
    $path = public_path(). "/uploads/" . $files['document_path'];

        if(file_exists($path)){
            $zip->addFromString(basename($path),  file_get_contents($path));
        }
        else{ echo"file does not exist"; }                  
    }
 $zip->close();
  • it is not possible to download multiple files at a time in laravel, but you can create a zip file of those files and download it – Pankaj Makwana Jul 18 '17 at 13:59

1 Answers1

1

You need to pass the $fileId within an array for more than one id

$file = Documents::findOrFail([$fileId]);
$number_of_files = count($file);
if ($number_of_files > 1) {
    // Push all the files in a zip and send the zip as download
} else {
   // Send the file as a download
   $file = public_path(). "/uploads/" . $file->document_path;
   return Response::download($file, 'filename.pdf');
}
linktoahref
  • 7,812
  • 3
  • 29
  • 51
  • Hm, now I get `ErrorException: Undefined index: document_path`. If I `dd($file)` I see that document_path exist –  Jul 18 '17 at 13:51
  • Probably you are getting more than one result. In that case you need to loop through `$file` – linktoahref Jul 18 '17 at 13:54
  • Yes, I get 1+ result because the ID's are 1+. Loop where? Inside the controller and how to pass it for download then? –  Jul 18 '17 at 13:55
  • I've added a loop but is downloading only 1 file instead of multiple –  Jul 18 '17 at 13:55
  • This may help you https://stackoverflow.com/questions/36675505/laravel-5-multiple-download-file – linktoahref Jul 18 '17 at 13:57
  • Okay, got it to download the zip but is always with one file inside –  Jul 18 '17 at 14:24
  • You need to check the count of results and if there is more than one then use zip else download the file directly – linktoahref Jul 19 '17 at 04:43
  • Thanks. Can you give me simple example of that? –  Jul 19 '17 at 05:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149545/discussion-between-linktoahref-and-user5996816). – linktoahref Jul 19 '17 at 05:56