1

I have a complex record stored in session. I want to update parts of this record in session then update the DB row with the session data. So far I have managed to overwrite the entire structure and not just the part of it I intend to.

Example:

base                <-- whole record
base.field1         <-- single field
base.field2         <-- single field
base.field3         <-- single field
base.users          <-- array of objects (users), stored as JSON column
base.details        <-- single dimensional array of fields
base.cards          <-- array of objects (cards), stored as JSON column
base.regions        <-- array of objects (regions), stored as JSON column

This whole structure is stored as a row in a progress table.

I load and store this data in session like this:

session()->put('base', Progress::find($id));

I then update some of the fields in the cards array:

$cards = session()->get('base.cards');
$cards[$index]['points'] = 100;

I then try (unsuccessfully) to update the session variable, having tried both below:

session()->put('base.cards', $cards);
session()->push('base.cards', $cards);
session('base.cards', $cards);

Then lastly I want to store this record in the progress table by updating its instance, like this:

Progress::find($id)->update(session()->get('base'));

How do I manipulate then update in session just one of the JSON/array fields?

UPDATE: I added:

session()->put('base.cards', $cards);
session()->save();

But still, when I dd(session()->get('base')) I get the $cards array?!

9
  • Where do you call Session::save() in your app? Commented Aug 29, 2017 at 10:57
  • I do not. Do I have to? Taylor's docs do not mention this? Commented Aug 29, 2017 at 10:59
  • Yes, you have to. Usually at the end of your program flow, when your app will 'terminate' you usually include a call to Session::save(). this will make sure the session data is written to the session driver. Commented Aug 29, 2017 at 11:03
  • Why do you need to store it in the session at all, if you read from the db at the beginning and persist it to the db after? Commented Aug 29, 2017 at 11:04
  • That is not the question. The question is how to manipulate / replace arrays in stored in the session structure. Commented Aug 29, 2017 at 11:06

2 Answers 2

4

I would suggest your problem is caused by you using Session in a way that was not intended. You seem to be storing an Eloquent model called Progress in the Session with the key 'base' and then expecting the get, put and push methods of Session to understand how to interact with your Model's parameters.

How about you make life simple and just store the ID in the Session...

$progress = Progress::find($id);
session()->put('progress_id', $progress->id );
session()->save();

And then when you want to pull it out to manipulate, save in database etc do this...

$progress_id = session()->get('progress_id');
$progress = Progress::find($progress_id);
$progress->cards = $cards->asJSON(); // Give your cards model a method that serialises itself
$progress->save();

Overall your code is now easier to read and everything's being used in a very 'normal' way. The session is just holding an ID, the model exists as a proper Eloquent model being accessed in the documented way.

Future you will be grateful when he comes to review this code :-)

Oh, and if you were storing the model in the session because you're worried about speed, pulling a single record from a properly indexed database is probably no slower than pulling the data from a session. We're talking thousands of a second but please do run tests to put your mind at ease.

Sign up to request clarification or add additional context in comments.

Comments

0

Complex structures supported by session storage is limited to arrays.

To benefit from . syntax in key names, you need to convert the model to array, e.g.:

session()->put('base', Progress::find($id)->toArray());

So you can do later

session()->put('base.cards', $cards);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.