I wanted to export the data based by id so it doesn't get all the data in the database, so example if the site is "http://127.0.0.1:8000/pengajuan/3" the "/3" is the 'submission_id' i wanted to export from, here's my code:
Controller
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');
}
}
How to do it?
CodePudding user response:
This code is for exportpdf()
function
// 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();
If error occurs, it can be from your PDF template (you can post error if it happens) since you directly using PDF file as a template, not the blade file