Laravel 5.3问题在会话数组中推送项目

时间:2019-04-10 14:38:09

标签: php laravel session

我在Laravel 5.3上的会话遇到了麻烦。问题是我要在一个会话数组中推送一个项目,但是检索该新项目时未列出该项目。我的方法如下:

    private function _updateNewCompanyProjectsList(Request $request, $info, $action)
{
    if (!$request->session()->has($this->new_company_projects_key)) {
        $request->session()->put($this->new_company_projects_key, []);
    }

    if($action =='add') {
        echo(json_encode($info).'<hr/>');
        $request->session()->push($this->new_company_projects_key, $info);
        die(json_encode($request->session()->get($this->new_company_projects_key))); // this doesn't show the newly added item, nor anywhere else
    }
    else {

        $items = $request->session()->get($this->new_company_projects_key);
        $id = $info['id_project'];
        $new_items = [];

        if(count($items) > 0){

            foreach ($items as $item){

                if($id == $item['id_project']){
                    if($action != 'delete'){
                        $new_items[] = $info;
                    }
                }
                else {
                    $new_items[] = $item;
                }
            }
        }

        $request->session()->forget($this->new_company_projects_key);
        $request->session()->put($this->new_company_projects_key, $new_items);
    }
}

此会话包含以下对象数组:

$info=[
        'id_project' => $id_project,
        'title_en' => $request->input('title_en'),
        'title_fr' => $request->input('title_fr'),
        'url_en' => $request->input('url_en'),
        'url_fr' => $request->input('url_fr'),
        'image' => $image
    ];

变量$id_project是一个UUID,它不同于数组中的现有变量。

我在这里可能做错了什么? 感谢您的帮助

0 个答案:

没有答案