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