我认为我做错了,因为它似乎太难了......基本上我将我的模型和关系id作为数组数组写入Session变量,我想在检索会话数据时加载模型在另一端。我可以以不同方式存储会话数据以使其更容易吗?
这是我存储数组的方式
$bookingsPaymentPending[$attendee_id][] = $program_id;
对于1位与会者(id=416
)和2位相关节目(id=2,4
),这给了我类似的内容
Array ( [416] => Array ( [0] => 2 [1] => 4 ) )
我试图使用Eloquent查询将其转回$attendees-with('programs')
及相关程序的集合
像
这样的东西foreach ($bookingPaymentPending as $attendee_id => $program_ids)
$attendee[]= Attendee::find($attendee)->with('program'=>function($q) use ($program_ids){
$q->whereIn('id', $program_ids);
}
`
但它不起作用,它也只是一个$attendees
的数组,我可能想要这个集合。
答案 0 :(得分:1)
您应该能够使用序列化实现此目的。
在存储对象之前对对象使用PHP serialize(),并反序列化()以重新安置它们。
http://php.net/manual/en/oop4.serialization.php
另见这里的文档:
http://laravel.com/docs/master/eloquent-serialization
但是,我真的不建议以这种方式存储数据。将您的雄辩对象保存到数据库中,如果您想在会话中使用它们,只需引用ID并在需要时从数据库中获取它。