Laravel 中的下载按钮,按钮上有多个文档 ID
Download button in Laravel with multiple documents ID's on button
我的查询 return 来自数据库的逗号分隔值结果:1,2,3..etc
然后我试图制作一个下载按钮,并且应该 select 1 个或多个要下载的文档(基于是 1 个 id 还是多个)。
所以只有一个文件的按钮看起来像这样
<a href="users/files/download/2?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
和查询 return 多个 ID 的按钮如下所示(注意标记前的 2,3
)
<a href="users/files/download/2,3?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
这个我加到routes.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: 没有模型 [Documents] 的查询结果。
这是什么意思?模型在那里。这是文档模型
class Documents extends Eloquent
{
protected $table = 'documents';
protected $primaryKey = 'id';
public $timestamps = false;
}
以及如何 select 所有文档 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();
您需要在一个数组内传递$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');
}
我的查询 return 来自数据库的逗号分隔值结果:1,2,3..etc
然后我试图制作一个下载按钮,并且应该 select 1 个或多个要下载的文档(基于是 1 个 id 还是多个)。
所以只有一个文件的按钮看起来像这样
<a href="users/files/download/2?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
和查询 return 多个 ID 的按钮如下所示(注意标记前的 2,3
)
<a href="users/files/download/2,3?_token=SivFIl3kKuflAvIyYJFGKdovJHTlqpjObN2nMFbQ">Download Now</a>
这个我加到routes.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: 没有模型 [Documents] 的查询结果。
这是什么意思?模型在那里。这是文档模型
class Documents extends Eloquent
{
protected $table = 'documents';
protected $primaryKey = 'id';
public $timestamps = false;
}
以及如何 select 所有文档 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();
您需要在一个数组内传递$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');
}