Laravel PDF导出:如何根据id导出数据
Laravel PDF Exports: How to export data based by id
我想根据 id 导出数据,这样它就不会获取数据库中的所有数据,例如,如果站点是“http://127.0.0.1:8000/pengajuan/3” “/3”是我想从中导出的 'submission_id',这是我的代码:
控制器
public function exportpdf()
{
$submissionDetail = SubmissionDetail::all();
$pdf = PDF::loadview('pengajuan_detail.pdf',['submissionDetail'=>$submissionDetail]);
return $pdf->stream();
}
SubmissionDetail.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SubmissionDetail extends Model
{
protected $fillable = [
'submission_id', 'nama_barang', 'image_path', 'jumlah', 'harga_satuan', 'harga_total', 'keterangan',
];
public function submission()
{
return $this->belongsTo('App\Submission');
}
public function negotiation()
{
return $this->hasOne('App\Negotiation');
}
public function realization()
{
return $this->hasOne('App\Realization');
}
}
怎么做?
此代码用于 exportpdf()
函数
// This will convert url string to array with '/' declimiter
$url = explode('/', url()->current()); // something like [..., '127.0.0.1:8000', 'pengajuan', '3']
$id = end($url); // result is 3
// Get Specific Submission detail with "where" function
$submissionDetail = SubmissionDetail::where('submission_id', $id)->first();
// Rest is just same
$pdf = PDF::loadview('pengajuan_detail', ['submissionDetail' => $submissionDetail]);
return $pdf->stream();
如果发生错误,它可能来自您的 PDF 模板(如果发生,您可以 post 错误)因为您直接使用 PDF 文件作为模板,而不是 blade 文件
我想根据 id 导出数据,这样它就不会获取数据库中的所有数据,例如,如果站点是“http://127.0.0.1:8000/pengajuan/3” “/3”是我想从中导出的 'submission_id',这是我的代码:
控制器
public function exportpdf()
{
$submissionDetail = SubmissionDetail::all();
$pdf = PDF::loadview('pengajuan_detail.pdf',['submissionDetail'=>$submissionDetail]);
return $pdf->stream();
}
SubmissionDetail.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SubmissionDetail extends Model
{
protected $fillable = [
'submission_id', 'nama_barang', 'image_path', 'jumlah', 'harga_satuan', 'harga_total', 'keterangan',
];
public function submission()
{
return $this->belongsTo('App\Submission');
}
public function negotiation()
{
return $this->hasOne('App\Negotiation');
}
public function realization()
{
return $this->hasOne('App\Realization');
}
}
怎么做?
此代码用于 exportpdf()
函数
// This will convert url string to array with '/' declimiter
$url = explode('/', url()->current()); // something like [..., '127.0.0.1:8000', 'pengajuan', '3']
$id = end($url); // result is 3
// Get Specific Submission detail with "where" function
$submissionDetail = SubmissionDetail::where('submission_id', $id)->first();
// Rest is just same
$pdf = PDF::loadview('pengajuan_detail', ['submissionDetail' => $submissionDetail]);
return $pdf->stream();
如果发生错误,它可能来自您的 PDF 模板(如果发生,您可以 post 错误)因为您直接使用 PDF 文件作为模板,而不是 blade 文件