phplaravellaravel-blade

Blade View | Get Array Data in structured rows | could someone check if my coding is ok in my controller


I'm trying to get the following data array structured into my blade file :

array:6 [▼
  "email" => "client_2.mage@webamazingapps.com"
  "name" => "Client Deux"
  "title" => "1"
  "firstname" => "Deux"
  "lastname" => "Client"
  "\x00*\x00items" => array:4 [▼
    0 => {#1685 ▼
      +"laboratory": "Cuisine"
      +"serving_persons": null
      +"desserttextmessage": null
      +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
      +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
      +"product_type": "Pain-surprise"
      +"product_name": "Mélangé"
      +"portions": "100"
      +"weight": "2.5 Kg"
      +"assortment_1": "Foie gras"
      +"assortment_2": "Gruyère"
      +"assortment_3": "Jambon"
      +"assortment_4": "Jambon cru"
      +"color": "Bleu"
      +"productprice": "89.00"
      +"productaddfee": "5.00"
      +"subtotal": "94.00"
      }
      1 => {#1688 ▼
      +"laboratory": "Pâtissier"
      +"serving_persons": "2-3 pers."
      +"desserttextmessage": "Inscription"
      +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
      +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
      +"product_type": "Gâteaux"
      +"product_name": "Chocolat"
      +"portions": null
      +"weight": null
      +"assortment_1": null
      +"assortment_2": null
      +"assortment_3": null
      +"assortment_4": null
      +"color": null
      +"productprice": "19.00"
      +"productaddfee": "5.00"
      +"subtotal": "24.00"
      }
      2 => {#1692 ▼
      +"laboratory": "Boulanger"
      +"serving_persons": "2 pers. | 16 cm"
      +"desserttextmessage": "Inscription Tarte"
      +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
      +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
      +"product_type": "Tarte"
      +"product_name": "Tarte fraises"
      +"portions": null
      +"weight": null
      +"assortment_1": null
      +"assortment_2": null
      +"assortment_3": null
      +"assortment_4": null
      +"color": null
      +"productprice": "11.00"
      +"productaddfee": "5.00"
      +"subtotal": "16.00"
      }
      3 => {#1690 ▼
      +"laboratory": "Boulanger"
      +"serving_persons": "2 pers. | 16 cm"
      +"desserttextmessage": "Inscription Tarte-fine"
      +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
      +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
      +"product_type": "Tarte-fine"
      +"product_name": "Pommes"
      +"portions": null
      +"weight": null
      +"assortment_1": null
      +"assortment_2": null
      +"assortment_3": null
      +"assortment_4": null
      +"color": null
      +"productprice": "13.00"
      +"productaddfee": "5.00"
      +"subtotal": "18.00"
    }
  ]
]

I know how to get the first 5 columns of my array (array:6) :

{{$content['email']}}
{{$content['name']}}
{{$content['title']}}
{{$content['lastname']}}
{{$content['firstname']}}

But I don't know how to start to get my four products (array:4) into rows like this :

If "laboratory" = "Cuisine" :

  +"laboratory": "Cuisine"
  +"serving_persons": null
  +"desserttextmessage": null
  +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
  +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
  +"product_type": "Pain-surprise"
  +"product_name": "Mélangé"
  +"portions": "100"
  +"weight": "2.5 Kg"
  +"assortment_1": "Foie gras"
  +"assortment_2": "Gruyère"
  +"assortment_3": "Jambon"
  +"assortment_4": "Jambon cru"
  +"color": "Bleu"
  +"productprice": "89.00"
  +"productaddfee": "5.00"
  +"subtotal": "94.00"

Labels and data should be displayed as followed :

laboratory  | product_type  | product_name  | portions  | weight    | assortment_1  | assortment_2 | assortment_3   | assortment_4  | color | productprice  | productaddfee | subtotal

Cuisine     | Pain-surprise | Mélangé       | 100       | 2.5 Kg    | Foie gras     | Gruyère   | Jambon    | Jambon cru    | Bleu  |  89.00    | 5.00  | 94.00

Columns 'serving_persons', 'desserttextmessage', 'dessertdecorchocolateID', 'dessertdecorflowerID' are not displayed because theu don't belong to "laboratory" = "Cuisine".

If "laboratory" = "Pâtissier" :

  +"laboratory": "Pâtissier"
  +"serving_persons": "2-3 pers."
  +"desserttextmessage": "Inscription"
  +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
  +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
  +"product_type": "Gâteaux"
  +"product_name": "Chocolat"
  +"portions": null
  +"weight": null
  +"assortment_1": null
  +"assortment_2": null
  +"assortment_3": null
  +"assortment_4": null
  +"color": null
  +"productprice": "19.00"
  +"productaddfee": "5.00"
  +"subtotal": "24.00"

Labels and data should be displayed as followed :

laboratory  | product_type  | product_name  | serving_persons   | desserttextmessage | dessertdecorchocolateID  | dessertdecorflowerID  | productprice  | productaddfee | subtotal

Pâtissier   | Gâteaux   | Chocolat  | 2-3 pers. | Inscription | Décoration chocolat et fruits souhaitée | Décoration petites fleurs souhaitée   | 19.00 | 5.00  | 24.00

Columns 'portions', 'weight', 'assortment_1', 'assortment_2', 'assortment_3', 'assortment_4', 'color' belongs not to "laboratory" = "Pâtissier".

If "laboratory" = "Boulanger" :

  +"laboratory": "Boulanger"
  +"serving_persons": "2 pers. | 16 cm"
  +"desserttextmessage": "Inscription Tarte"
  +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
  +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
  +"product_type": "Tarte"
  +"product_name": "Tarte fraises"
  +"portions": null
  +"weight": null
  +"assortment_1": null
  +"assortment_2": null
  +"assortment_3": null
  +"assortment_4": null
  +"color": null
  +"productprice": "11.00"
  +"productaddfee": "5.00"
  +"subtotal": "16.00"

Labels and data should be displayed as followed :

laboratory  | product_type  | product_name  | serving_persons   | desserttextmessage | dessertdecorchocolateID  | dessertdecorflowerID  | productprice  | productaddfee | subtotal

Pâtissier   | Gâteaux   | Chocolat  | 2-3 pers. | Inscription | Décoration chocolat et fruits souhaitée | Décoration petites fleurs souhaitée   | 19.00 | 5.00  | 24.00

Columns 'portions', 'weight', 'assortment_1', 'assortment_2', 'assortment_3', 'assortment_4', 'color' belongs not to "laboratory" = "Boulanger".

Products can be more than 3, theoretically unlimited.

Here how I fetch my data into the controller :

            # START SEND EMAIL TO X

            $customer = $_REQUEST['userID'];

            // dd($customer);

            $customerdata = DB::table('cms_users')
                                ->leftJoin('users_r3_details','cms_users.id', '=', 'users_r3_details.userID')
                                ->select('cms_users.email','cms_users.name','cms_users.email','users_r3_details.title','users_r3_details.firstname','users_r3_details.lastname')
                                ->where('cms_users.id', '=', $customer)
                                ->first();

            // dd($customerdata);

            $orderdata = DB::table('orders_detail')
                                ->leftJoin('dessert_servings','orders_detail.dessertservingID', '=', 'dessert_servings.id')
                                ->leftJoin('product_types','orders_detail.producttype_ID', '=', 'product_types.producttypeID')
                                ->leftJoin('product_names','orders_detail.productname', '=', 'product_names.productname_ID')
                                ->leftJoin('party_loaf_portions','orders_detail.ID_portion', '=', 'party_loaf_portions.portion_ID')
                                ->leftJoin('party_loaf_weights','orders_detail.partyloafweightID', '=', 'party_loaf_weights.id')
                                ->leftJoin('party_loaf_sandwiches_1','orders_detail.partyloafsandwich1ID', '=', 'party_loaf_sandwiches_1.id')
                                ->leftJoin('party_loaf_sandwiches_2','orders_detail.partyloafsandwich2ID', '=', 'party_loaf_sandwiches_2.id')
                                ->leftJoin('party_loaf_sandwiches_3','orders_detail.partyloafsandwich3ID', '=', 'party_loaf_sandwiches_3.id')
                                ->leftJoin('party_loaf_sandwiches_4','orders_detail.partyloafsandwich4ID', '=', 'party_loaf_sandwiches_4.id')
                                ->leftJoin('party_loaf_ribbons','orders_detail.partyloafribbonID', '=', 'party_loaf_ribbons.id')
                                ->select('orders_detail.laboratory','product_types.product_type','product_names.product_name','dessert_servings.serving_persons','orders_detail.desserttextmessage','orders_detail.dessertdecorchocolateID','orders_detail.dessertdecorflowerID','party_loaf_portions.portions','party_loaf_weights.weight','party_loaf_sandwiches_1.assortment as assortment_1','party_loaf_sandwiches_2.assortment as assortment_2','party_loaf_sandwiches_3.assortment as assortment_3','party_loaf_sandwiches_4.assortment as assortment_4','party_loaf_ribbons.color','orders_detail.productprice','orders_detail.productaddfee','orders_detail.subtotal')
                                ->where('orders_detail.orderID', '=', $id)
                                ->get();

            // dd($orderdata);

            $orderconfuserdataArray = (array) $customerdata;

            $orderconforderdataArray = (array) $orderdata;

            // dd($orderconfuserdataArray);
            // dd($orderconforderdataArray);

            $orderconfdatas = array_merge($orderconfuserdataArray, $orderconforderdataArray);

            // dd($orderconfdatas);

            $email = $customerdata->email;
            $copyto ='';

            // dd($email);

            try {

                // Example : Mail::to($request->user())->cc($moreUsers)->bcc($evenMoreUsers)->queue(new OrderShipped($order));

                Mail::to($email)->send(new CustomerConfOrder($orderconfuserdataArray,$orderconforderdataArray));
                echo 'Commande et confirmation de commande expédiées avec succès';

            } catch (\Exception $e) {

                echo 'Error - '.$e;
                sleep(150);

            }

            # END SEND EMAIL TO X

I would appreciate your expertise and advice how to tackle this.

Cheers, Marc

=========================================================================

UPDATE :

My arrays looks more conventional now after a minor change in my code :

array:2 [▼
  0 => {#1727 ▼
    +"email": "client_1.mage@webamazingapps.com"
    +"name": "Client Un"
    +"title": "1"
    +"firstname": "Un"
    +"lastname": "Client"
  }
  1 => Collection {#1737 ▼
    #items: array:1 [▼
      0 => {#1733 ▼
        +"laboratory": "Boulanger"
        +"product_type": "Tarte"
        +"product_name": "Tarte fraises"
        +"serving_persons": "2 pers. | 16 cm"
        +"desserttextmessage": "Inscription"
        +"dessertdecorchocolateID": "Décoration chocolat et fruits souhaitée"
        +"dessertdecorflowerID": "Décoration petites fleurs souhaitée"
        +"portions": null
        +"weight": null
        +"assortment_1": null
        +"assortment_2": null
        +"assortment_3": null
        +"assortment_4": null
        +"color": null
        +"productprice": "11.00"
        +"productaddfee": "5.00"
        +"subtotal": "16.00"
      }
    ]
  }
]

I tried again my foreach :

@foreach($Collection as $row)
    {{$row->laboratory}}
    {{$row->serving_persons}}
    {{$row->desserttextmessage}}
@endforeach

Or more simply :

{{$content['Collection']}}

to get my content, but again no data is printed into the email.

Would need a hand here, since I'm stuck. Thanks, Marc


Solution

  • SOLUTION :

    Finally found the solution how to make the foreach work in a structured form :

    <table style="width:100%">
    <tbody style="width: 100%">
        <?php $three=0; ?>
        @foreach($content['item'] as $contacts)
    
        @if($three%3==0)
        <tr style="width: 100%">
        @endif
            <td style="width: 33.3%;border: 1px solid #ded3d3;padding: 15px 15px;">
    
            <?php $protype='';  ?>
            @foreach($contacts as $check => $val)
              <p style="margin: 5px 2px;">
    
    
    
                <?php if($check=='product_type'&&$val=='Pain-surprise'){ $protype=$val; } ?>
    
    
    
                @if($check=='productaddfee'&&$protype=='Pain-surprise')
    
    
                @elseif($check=='dessertdecorchocolateID'&&$protype=='Pain-surprise')
                @elseif($check=='dessertdecorflowerID'&&$protype=='Pain-surprise')
                @else
    
                    @if($val!=''&&$val!=' ') 
                    <span style="font-size: 18px;text-transform: capitalize;font-weight: 600;font-family: monospace;color:#b7252b">{{ $check }}</span> : 
                    <span style="font-size: 15px;font-family: sans-serif;text-transform: capitalize;letter-spacing: 1px;font-weight: 600;">{{ $val }}</span>
                    @endif 
                @endif
    
               </p>
    
            @endforeach
        </td>
    
    @if($three%3==0&&$three!=0)
    </tr>
    @endif
    <?php $three++; ?>
        @endforeach
    </tbody>