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

Danny971's avatar

Display selected items by category in Laravel Blade PDF view

'm working on a Laravel project where users can select items from different categories, and I need to display the selected items in a PDF generated using Laravel Blade. I have categories like Living Room, Bedding, Kitchen, etc., and users can choose items from these categories.

Here's how I'm currently attempting to display the selected items:



 $pdf = PDF::loadView('pdf_layout', [

    'headofhousehold_firstname' => $firstname,
    'headofhousehold_lastname' => $lastname,
    'phone' => $phone_num,
    'address' => $address,
    'address2' => $address2,
    'City' => $city,
    'State' => $state,
    'Zip' => $zip,
    'numofadults' => $numAdults,
    'numofkids' => $numkids,
    'agency' => $agency,
    'advocatefirstName' => $advocate_FirstName,
    'advocatelastName' => $advocate_Lastname,
    'Gender' => $gender,
    'MaritalStatus' => $maritalstatus,
    'receivedDate' => $received_date,
    'MoveInDate' => $moveInDate,
    'adult_first_names' => $adult_first_names,
    'adult_last_names' => $adult_last_names,
    'adult_age' => $adult_age,
    'adult_gender' => $adult_gender,
    'child_first_names' => $child_first_names,
    'child_last_names' => $child_last_names,
    'child_age' => $child_age,
    'child_gender' => $child_gender,
    'livingroomOutputString' => $livingroomOutputString ? 'Living Room: ' . $livingroomOutputString : '',
    'beddingOutputString' => $beddingOutputString ? 'Bedding: ' . $beddingOutputString : '',
    'bedroomOutputString' => $bedroomOutputString ? 'Bedroom: ' . $bedroomOutputString : '',
    'kitchenOutputString' => $kitchenOutputString ? 'Kitchen: ' . $kitchenOutputString : '',
    'miscellaneousOutputString' => $miscellaneousOutputString ? 'Miscellaneous: ' . $miscellaneousOutputString : '',
    'babyOutputString' => $babyOutputString ? 'Baby: ' . $babyOutputString : '',


 ]); 

 $pdfdata = $pdf ->output();


here is my blade file

<table>
    <thead>
        <tr>
            <th>CATEGORY</th>
            <th>ITEM</th>
            <th>NEED</th>
        </tr>
    </thead>
    <tbody>
         @foreach($selectedCategories as $category => $items)
        <tr>
            <td rowspan="{{ count($items) }}">{{ $category }}</td>
            @foreach($items as $item)
            <td>{{ $item['name'] }}</td>
            <td>{{ $item['quantity'] }}</td>
            @endforeach
        </tr>
    </tbody>

</table>


here is an  example of how get the amount request form the request from but i need that data on the pdf 

   
  // ***** condition if any item from BABY category was selected *****
       
       $babyQuantities = $_POST['babyitems'] ??[];
       
       $categoryTotal = array_sum($babyQuantities);
       
      // dd("baby quantities:" ,$babyQuantities, "Category Total:", $categoryTotal);
       $babyItems = DB::table('baby')
               ->whereIn('id',array_keys($babyQuantities)) // get the requested items
               ->get();
       $babyOutputs =[]; // this array will hold the output for each item
       
       
       
       foreach($babyQuantities as $id => $babyQuantity) {
           if ($babyQuantity > 0) {
               
               $babyItem = $babyItems -> where('id',$id)->first();
               
               if( $babyItem) {
                   
                    $babyItemsName = $babyItem -> baby_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'BABY',
                        
                  'categoryTotal' => array_sum($babyQuantities),
                  
                  'requestedItem' => $babyItemsName,
                  'amountNeeded' => $babyQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $babyOutput = $babyItem -> baby_item. ':' .$babyQuantity;
                    $babyOutputs[] =$babyOutput; //append the output to the array 
               }
           }
       }
       
        
       
       $babyOutputString = implode( $babyOutputs);
0 likes
2 replies
amitsolanki24_'s avatar

You can also pass selected categories into loadView() function inside 2nd parameter

Please or to participate in this conversation.