简体   繁体   English

Laravel会话变量未更新

[英]Laravel session variable is not updating

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. 整个结构作为一行存储在progress表中。

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数组中的某些字段:

$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? 如何处理然后在会话中仅更新JSON / array字段之一?

UPDATE: I added: 更新:我添加了:

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

But still, when I dd(session()->get('base')) I get the $cards array?! 但是仍然,当我dd(session()->get('base'))我得到$cards数组?

I would suggest your problem is caused by you using Session in a way that was not intended. 我建议您的问题是由于您以非预期方式使用Session引起的。 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. 您似乎在会话中使用键“ base”存储了一个称为Progress的Eloquent模型,然后使用Session的getputpush方法来了解如何与Model的参数进行交互。

How about you make life simple and just store the ID in the Session... 您如何简化生活,只将ID存储在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. 会话仅持有一个ID,该模型作为以文档方式访问的适当的Eloquent模型而存在。

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. 我们正在谈论数千秒,但请进行测试以使您放心。

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, eg: 键名中的语法,您需要将模型转换为数组,例如:

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

So you can do later 所以你以后可以做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM