简体   繁体   English

如何将会话值存储区放入数据库?

[英]How to put session value store into database?

I wanted to retrieve session array and put into other model But throws ` 我想检索会话数组并放入其他模型,但是抛出`

Call to a member function pluck() on array 在数组上调用成员函数pluck()

Controller I used : 我使用的控制器:

$orders =  $request->session()->get('order');
$order = new Order();
$order->school_id = $orders->pluck('school_id');
$order->order_date = $orders->pluck('order_date');
$order->time_slot = $orders->pluck('time_slot');

How do i access the session data and put into other model? 如何访问会话数据并放入其他模型?

Here is the response I get when i dd() the session : 这是我在dd()会话时得到的响应:

array:1 [▼
  0 => array:3 [▼
    "school_id" => "4"
    "order_date" => "11/25/2017"
    "time_slot" => "10am - 8pm"
  ]
]

try like this, 这样尝试

$orders =  $request->session()->get('order');
print_r($orders);

if you getting orders of school id array then you can get it by $orders['school_id']; 如果您获得学校ID数组的订单,则可以通过$ orders ['school_id'];获得; and if you getting std object then you can retrieve it by $orders->school_id; 如果您获得了std对象,则可以通过$orders->school_id;检索它$orders->school_id; Use as per output of print_r(orders) Then you can store it by 按照print_r(orders)输出使用,然后可以通过

If std object :: 如果std对象::

$order = new Order();
$order->school_id = $orders[0]->school_id;
$order->order_date = $orders[0]->order_date;
$order->time_slot = $orders[0]->time_slot;
$order->save();

If array :: 如果数组::

$order = new Order();
$order->school_id = $orders[0]['school_id'];
$order->order_date = $orders[0]['order_date'];
$order->time_slot = $orders[0]['time_slot'];
$order->save();

I would use the array_get helper. 我会使用array_get助手。

https://laravel.com/docs/5.5/helpers#method-array-get https://laravel.com/docs/5.5/helpers#method-array-get

array_get($orders, 'school_id');

Additionally you can use a fallback as third parameter in case the value is not present in the session. 此外,如果会话中不存在该值,则可以将回退用作第三个参数。

Session holds all data in Array and not Collection. 会话将所有数据保存在数组中,而不保存在集合中。 Function pluck() can only be used for the collection. 函数pluck()仅可用于集合。

Try doing array_get($orders, 'school_id'); 尝试做array_get($orders, 'school_id'); as already mentioned by Marc or just $request->session()->get('order')['school_id']; 正如Marc已经提到的那样,或者只是$request->session()->get('order')['school_id'];

it depends on how did you store it into session, so if you used push, then it will be stored as array. 这取决于您如何将其存储到会话中,因此,如果使用了push,则它将存储为数组。

anyways change your code to the following: 无论如何,请将您的代码更改为以下内容:

$orders =  $request->session()-> pull('order',$defaultOrdersArrayCanBeHere);
$order = new Order();
$order->school_id = $orders['school_id'];
$order->order_date = $orders['order_date'];
$order->time_slot = $orders['time_slot'];

UPDATE: 更新:

Did you push it to session as so: session()->push('order', $order); 您是否按以下方式将其推送到会话: session()->push('order', $order);

where: 哪里:

$order = [
'school_id'=> $schoolId;
'order_date'=> $orderDate;
'time_slot'=> $timeSlot;
]

Call to a member function pluck() on array 在数组上调用成员函数pluck()

Your $orders variable is an array so you can't use pluck. 您的$orders变量是一个数组,因此不能使用pluck。 You can do $order['key'] to access their values. 您可以执行$order['key']来访问其值。 Or as others suggested, use the helper function array_get 或如其他建议那样,使用辅助函数array_get

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

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