Laravel and Vue.js accessing a session variable. Is there a better way?

Viewed 218

I need to store a session variable in my app. In the Laravel controller I do something like this:

if (!session('xyz')) {
  session(['xyz' => 'ABC']);
}

Since I'm using Inertia, I render the component like:

return Inertia::render('Comp')
    ->with('xyz', session('xyz'));

Then in the componet I read the session variable with this.$page.props.xyz

The code works, but since I'm pretty novice in VueJS, I was wondering if this is correct, or if there's some other better way.

Right now than I'm using a pair of variables, I think is okay, but having more and more, my code will have lots of ->with()s

Related to this, I read this question: Access session data from Vuejs but I didn't know where to use the this.$session.start(), I placed it in the mounted() section of my component, but it threw an error: app.js:20517 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'start')

So I feel there must be a better way, I think using the $session object might be the right thing to do.

0 Answers
Related