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

alex32's avatar
Level 2

Laravel Inertia share array

I'm new to Inertia and so far I was only able to share a single string as flash data via a session variable and middleware. That works, but How do I share an array ?

I tried with the same mechanism, then without middleware directly with Inertia::share() but nothing worked. Do I need to JSON it ? Thanks

  • laravel 11
  • Breeze-React

Controller.php:

$recovery_codes= ['aaaaaaa43', 'bbbbbbb23', 'ccccccc80']; 
Inertia::share('arrMsg', $recovery_codes);

Form.jsx | 1st try

const   {arr}   = usePage().props;  

{arr.arrMsg && (
   <div class="alert">{arr.arrMsg}  </div>
)} 

Form.jsx | 2nd try

const   {arr}   = usePage().props;  

let msgList = [];
    arr.arrMsg.forEach((key,index) => {
    msgList.push(<li >{key} - {index}</li>);
});

{arr.arrMsg && (
 <div><ul>
   {msgList}
 </ul></div>
)} 

Error: Cannot read properties of undefined (reading 'arrMsg')

https://inertiajs.com/shared-data

0 likes
6 replies
EveAT's avatar

The Inertia::share() function makes arrMsg directly available in usePage().props. So try this instead:

const { arrMsg } = usePage().props;

{arrMsg && (
  <ul className="alert">
    {arrMsg.map((code, index) => (
      <li key={index}>{code}</li>
    ))}
  </ul>
)}
JussiMannisto's avatar

You're trying to destructure an arr property which doesn't exist in usePage().props. Like @eveat showed, you can extract arrMsg directly. Either of these works:

const { arrMsg } = usePage().props;
const arrMsg = usePage().props.arrMsg;
1 like
alex32's avatar
Level 2

Thanks both, I did what you said Eve. No errors this time, but no data either.

I've a doubt: how Inertia::share() knows which component "arrMsg" should be sent to? Its arguments are only the variable "arrMsg" and it's value $recovery_codes.

JussiMannisto's avatar

@alex32 Inertia sends the data as part of the (Inertia) response. It's not sent to any component in particular, it's accessible through the usePage() hook everywhere.

You might use Inertia::share in a middleware, but I don't see a reason to use it in a controller. You can pass the props directly in the render call, e.g.:

return Inertia::render('Some/Page', [
	'arrMsg' => ['foo', 'bar']
]);

They can be accessed through usePage() the same way.

alex32's avatar
alex32
OP
Best Answer
Level 2

Thanks, now it works with arrays too. I've done as @eveat showed, but using
session()->get('arrMsg') in the Middleware and session(['arrMsg'=> $recovery_codes ] ) in the Controller - still not working with Inertia::Share() - never mind I'll figure out..

JussiMannisto's avatar

@alex32 Why didn't you give @eveat the best answer? Your own answer added nothing.

The middleware/controller approach you showed can't work. You're trying to set the session value in the controller and read it in the middleware. If the middleware is executed first, then the value won't be there - or it will be a stale value from a previous request. And if the middleware is executed after the controller method, then the Inertia response has already been constructed. Sharing the data won't work in either case.

Just pass the data in the controller like I showed, or use share() in a middleware before the controller method is called.

Please or to participate in this conversation.