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

tasninta's avatar

Put post request in session

Hi, I want to do the following:

Post my request in to a session this is for a shopping cart. I want to post the id of the product and the quantity in the session.

This is my view:

@extends('layouts.app')

@section('content')
    <div class="container">
        <h1>Speelgoed:</h1>
        @if(Session::has('id', 'number'))
            <div class="alert alert-success">
                {{Session::get('id', 'number')}}
            </div>
        @endif
        @foreach ($products as $product)
            <ul>
                <li><h3>{{ $product->name }}</h3></li>
                <li>{{ $product->description }}</li>
                <li>€ {{ $product->price }}</li>
                <li>Op voorraad: {{ $product->amount }}</li>
                <form method="post" action="{{url('categories\{id}')}}">
                    @csrf
                    <div class="col-md-4">
                    <label for="amount">Aantal:</label>
                    <input type="number" name="number">
                    <input type="hidden" id="id" name="id" value=id>
                </div>
                    <button type="submit" class="btn btn-success">Add product</button>
                </form>
                {{var_dump(Session::get('cart'))}}
            </ul>
        @endforeach
    </div>
@endsection

This is my controler:

<?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)
  {
    $request->input('id', $id);
    $request->input('number', $number);


    return redirect('categories/'.$id);
  }
}

These are my routes:

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

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

0 likes
3 replies
tasninta's avatar

I can add the product to the session that is not the problem, but I have issues with adding the quantity to the session because that is what the user have to give up in the text field.

@tykus

tykus's avatar

I don't know what's going on with the form and controller method to be honest... there is so much wrong!

  • Why does your method expect $number as an argument; you are posting form data?
  • Also, what id should be in the session; product id or category id???
  • Why are you including an id in the URI?
  • What is with the value attribute on the hidden field in the form - that is not valid?
  • The form action url has a backslash \ rather than a forward slash /?
<form method="post" action="{{url('categories\{id}')}}">
    @csrf
    <div class="col-md-4">
        <label for="amount">Aantal:</label>
        <input type="number" name="number">
        <input type="hidden" id="id" name="id" value=id> // value is not valid
    </div>
// ...

Something like this should work once you fix all of the other issues.

public function add(Request $request, $id)
{
    session()->put('id', $request->post('id'))
    session()->put('number', $request->post('number'));

    return redirect('categories/'.$id);
  }

Please or to participate in this conversation.