vendredi 23 mars 2018

Laravel - Foreach in session and get attributes

I'm currently making a system where I'm working with sessions before I put the values in the database to prevent the mess. Now I have a small problem. If the user gets to a certain controller, All the values that were stored in the session need to be stored in the database. My sessions look like this:

enter image description here

If the GIF is too small, here is an imgur link: https://imgur.com/a/uUxcI

Now I need to have a foreach loop that loops through all the themes and products sessions and get their price and ID. But because they're stored in the session I have no clue how to retrieve them.

I store them in the session like this:

Themes

$theme = Product::find($selectedTheme);

    foreach($this->predefinedArray as $value) {
        $request->session()->put('chosen_theme.' . $value, $theme->$value);
    }

Plugins

foreach($request->input('plugin') as $key => $value) {
        if ($value === 'selected') {
            $selectedPlugin = $key;

            $plugin = Product::find($selectedPlugin);

            $price += $plugin->price;

            echo "ID: " . $plugin->id . "<br>";
            $selectedPlugins[$plugin->id] = $plugin->toArray();

            $request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id, $plugin->toArray());
            $request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id .'.composer_package', $plugin->productable->composer_package);

        }
    }

I know how to get a value out of a single session, like this $themePrice = Session::get('chosen_theme')['price']; But I don't know how to loop through a session with arrays

This is as far as I got

foreach( It needs to loop through all the sessions ) {

       $orderItems = new Orderitems;

        $orderItems->order_id = $order->id;
        $orderItems->product_id = ;
        $orderItems->price = ;
}

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire