Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tasninta's avatar

Post request in session

Hi, I am trying to add the quantity of the product in the session. I can post the id of the product in the session, but I can't post the quantity in the session.

I hope someone can help me out.

My view:

@extends('layouts.app')

@section('content')
    <div class="container">
        <h1>Speelgoed:</h1>
        @if(Session::has('status'))
            <p class="alert alert-success">{{ Session::get('status') }}</p>
        @endif
        @foreach ($products as $product)
            <ul>
                <li>{{ $product->name }}</li>
                <li>{{ $product->description }}</li>
                <li>{{ $product->price }}</li>
                <li>{{ $product->amount }}</li>
                {{var_dump(Session::get('cart'))}}
                <lable>Hoeveelheid:</lable>
                <input type="text" name="number">
                <a href="{{action('ShopController@add', ['number' => $product->number, 'id' => $product->id])}}" type="submit" value="Submit" class="btn btn-success">Add</a>
            </ul>
        @endforeach
    </div>
@endsection

My controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;
use \App\Product;

class ShopController extends Controller
{
  public function index()
  {
    $categories = Category::with('products')->get();
    return view('shop.index', compact('categories'));
  }

  public function category($id)
  {
    $products = Category::find($id)->first()->products;
    return view('shop.1', compact('products'));
  }

  public function add(Request $request, $id, $number)
  {
    $cart = $request->session()->get('cart');
    if (!$cart){
      $cart = [];
    }
    $cart[] = $id;
    $cart[] = $number;
    $request->session()->put('number', $number);
    $request->session()->put('cart', $cart);
    $request->session()->flash('status', 'Product is toegevoegd aan de winkelwagen!');
    return redirect()->back();
  }
}

My routes:

Route::get('/cart/{id}', 'ShopController@add');

0 likes
3 replies
jlrdw's avatar

For an array and session try push, from docs:

Pushing To Array Session Values

The push method may be used to push a new value onto a session value that is an array. For example, if the user.teams key contains an array of team names, you may push a new value onto the array like so:

$request->session()->push('user.teams', 'developers');

Re-read session in docs:

https://laravel.com/docs/5.7/session#storing-data

Snapey's avatar

You have this

  public function add(Request $request, $id, $number)

so you are expecting id and number to be passed to the server, but in the route;

Route::get('/cart/{id}', 'ShopController@add');

I see no number there?

try;

Route::get('/cart/{id}/{number}', 'ShopController@add');

but you won't win any programming points for using a GET route to update the server. Your page should POST with a form and not use an anchor.

Snapey's avatar

Also, install Laravel Debugbar. It will show you exactly what is in session.

Please or to participate in this conversation.