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

KingsleyO's avatar

Array to String conversion error using session in laravel

I am trying to dynamically populate an invoice(wrapper for DomPDF) with jquery dynamically generated invoice. First of all on a different page saving the inputs to sessions and with a "redirect to" the invoice page generate the invoice. Long story short i have this to put the values

  
    Session::put('invoice-data1', post('product'));
    Session::put('invoice-data2', post('qty'));
    Session::put('invoice-data3', post('price'));
    Session::put('invoice-data4', post('total'));
    Session::put('invoice-data5', post('sub_total'));
    Session::put('invoice-data6', post('tax_amount'));
    Session::put('invoice-data7', post('total_amount'));

      return Redirect::to('invoice');

fields like product, price, qty are this way in the input field expecting an array

<input type="text" name="product[]" />

and then to get the the session value in the invoice page i have

 $product = Session::pull('invoice-data1');
     $qty = Session::pull('invoice-data2');
      $price = Session::pull('invoice-data3');
       $total = Session::pull('invoice-data4');
       $subTotal = Session::pull('invoice-data5');
       $taxAmount = Session::pull('invoice-data6');
       $totalAmount = Session::pull('invoice-data7');


      $templateCode = 'renatio::invoice'; // unique code of the template
    $data = [
    'product' => $product,
      'qty' => $qty,
        'price' => $price,
          'total' => $total,
            'subTotal' => $subTotal,
              'taxAmount' => $taxAmount,
                'totalAmount' => $totalAmount


    ]; // optional data used in template

    return PDF::loadTemplate($templateCode, $data)->stream('download.pdf');

Trying it with just a regular input field works without an issue but this way it throws up an error of

"Array to string conversion"

A little confused how to get the session in my php code or in twig which i am using

0 likes
4 replies
Snapey's avatar

Can you fix your code blocks please?

Why are you obfuscating the data as 'invoice-data1` etc? Why not use the actual names?

Why not just store in session as an array?

Session::put('invoice',$post);

Do you have multiple products in the $post? Not sure why you are using form array syntax?

You would probably find renaming your form elements a big help. ie;

    <input type="text" name="item[]product" />

    <input type="text" name="item[]qty" />

    <input type="text" name="item[]price" />

etc

Then you will have an array of items that you can iterate over, each having attributes of product, qty etc, instead of having independent arrays that you have to use an index counter with

KingsleyO's avatar

Well the solution is as simple as

 {% for product in product %}
        {{ product }}<br>
       {% endfor %}

in twig

Snapey's avatar

and changing all the field names over

.... and you did not even show the twig code...

KingsleyO's avatar

Thanks, I have made those changes. With the twig. I thought i could fix it in the controller using foreach. Was just confused on to write it. But it works Ok with twig

Please or to participate in this conversation.