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

DoppyWoppy's avatar

How to return an array with redirect()->back()->with('',[$a]);

I want to return an array to blade file and display. but I am getting an error : " htmlspecialchars() expects parameter 1 to be string, array given "

here is my code:

     $return = print_r('Data inserted for record id:' . $row['record_id'],true);
 return redirect()->back()->with('msg', [$return]);

blade file: @if(session()->has('msg')) {{ session()->get('msg') }} @endif Thanks.

0 likes
6 replies
Snapey's avatar

print_r ?

    $msg= "Data inserted for record id: {$row['record_id']}";

    return redirect()->back()->with(compact('msg');

DoppyWoppy's avatar

I am getting only one record " Data inserted for record id:' 1879 ".

DoppyWoppy's avatar

I storing a values in an array in foreach loop. And then I am passing that array to blade file @if(session()->has('msg')) {{ session()->get('msg') }} @endif but in blade file I am getting only one record, not all the arrray values.

DoppyWoppy's avatar
DoppyWoppy
OP
Best Answer
Level 1

This is the solution that helped me with this issue.

  @foreach(session()->get('msg')  as $in)
          <tr>
                   <td>{{$in}}</td>
            </tr>
   @endforeach

Please or to participate in this conversation.