我的查询以逗号分隔值的形式从数据库返回结果:<a href="users/files/download/2?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
然后我尝试下载按钮,并选择下载1个或多个文档(基于if是1个id还是多个)。
所以带有一个文件的按钮看起来像这样
2,3
和查询返回多个ID的按钮看起来像这样(注意令牌前的<a href="users/files/download/2,3?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
)
Route::get('/users/files/download/{fileId}', 'UsersController@getDownload');
这是我添加到routes.php
public function getDownload($fileId)
{
$file = Documents::findOrFail($fileId);
$file = public_path(). "/uploads/" . $file->document_path;
return Response::download($file, 'filename.pdf');
}
这是控制器
class Documents extends Eloquent
{
protected $table = 'documents';
protected $primaryKey = 'id';
public $timestamps = false;
}
目前无论我点击哪个按钮,我都有
Illuminate \ Database \ Eloquent \ ModelNotFoundException:没有模型[Documents]的查询结果。
这是什么意思?那个模型就在那里。这是文档模型
$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();
如何在多个文件ID选择时选择所有文件?
更新:当前代码
{{1}}
答案 0 :(得分:1)
您需要在数组中传递$fileId
以获取多个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');
}