Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

qyioma's avatar

Print pdf

I want to have three label in one pdf file with the same information. But, I don't know how to make it loop. I try to use loop in my controller, but the label only shows one in the pdf file, not three. Can someone help me to solve this issues. Below is my code for controller.

public function label_BALER(){

$printdate = Carbon::now();

$labels = Baler::orderBy('id','desc')->first();
//  for($i=0; $i<4; $i++) {
$pdf = WkPDF::loadView('frontend.praiBarcode.baler_label',  compact('labels','printdate'))

     ->setOption('page-width', '105')
     ->setOption('page-height','120');
  
return $pdf->stream(date('Y_m_d H:i.')."$labels->work_order");

// };

}

below is my code for blade:

{!! DNS1D::getBarcodeHTML('53626536','C128',3,56);!!} {!! \QrCode::size(60)->generate('/praiBarcode/label_BALER'); !!}

BARCODE ID: {{$labels->label_ID}}

WORK ORDER NO: {{$labels->work_order}}

PART NO: {{$labels->part_no}}

INVOICE: {{$labels->invoice}}

RECEIVING DATE: {{$labels->date_received}}

TOTAL WT: {{$labels->total_weight}} KG

TOTAL CUBE BLOCK: {{$labels->cube_block}}

AVERAGE WT: {{$labels->average}} KG

PRINTED ON {{$printdate}} @ RO

0 likes
6 replies
Lara_Love's avatar

are you use ? :

composer require barryvdh/laravel-dompdf
azimidev's avatar
azimidev
Best Answer
Level 55

To make the label show three times in the PDF file, you need to loop through the data in the view and display each iteration.

In the controller, you need to retrieve the data for all the labels that you want to display, not just one. For example, you can retrieve 3 labels using Baler::orderBy('id','desc')->take(3)->get();

In the view, you can use a for loop to display each label data. Here is an example:

@for($i=0; $i<3; $i++)
  {!! DNS1D::getBarcodeHTML('53626536','C128',3,56);!!} 
  {!! \QrCode::size(60)->generate('/praiBarcode/label_BALER'); !!}
  BARCODE ID: {{$labels[$i]->label_ID}}

  WORK ORDER NO: {{$labels[$i]->work_order}}

  PART NO: {{$labels[$i]->part_no}}

  INVOICE: {{$labels[$i]->invoice}}

  RECEIVING DATE: {{$labels[$i]->date_received}}

  TOTAL WT: {{$labels[$i]->total_weight}} KG

  TOTAL CUBE BLOCK: {{$labels[$i]->cube_block}}

  AVERAGE WT: {{$labels[$i]->average}} KG

  PRINTED ON {{$printdate}} @ RO
@endfor

1 like
Ben Taylor's avatar

If you want the same label 3 times you can use range.

@foreach(range(0, 2) as $number) 

Your label html

@endforeach

Or if you want three different labels, get them the way @azimidev suggested. But tidier to loop this way

@foreach($labels as $label)
1 like

Please or to participate in this conversation.