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

tasninta's avatar

How to remove single line of array in session

Hi I am trying to remove a single line in my array it is stored in a session

I hope someone can help me out with deleting the single line

Shop controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

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)->products;
    return view('shop/product', compact('products'));
  }

  public function addToShoppingCart(Request $request) {
    $data = $this->validate($request, [
      'id' => 'required|integer|min:1|max:2147483647',
      'amount' => 'required|integer|min:1|max:100',
    ]);

    $currentCart = $request->session()->get('cart');

    if (!is_array($currentCart)) {
      $currentCart = [];
    }

    if (array_key_exists($data['id'], $currentCart)){
      $currentCart[$data['id']]+= $data['amount'];
    }else{
      $currentCart[$data['id']] = $data['amount'];
    }

    $request->session()->put('cart', $currentCart);
    return Redirect()->action('ShopController@cart');
  }

  public function clear(Request $request) {
    $request->session()->flush();
    return Redirect()->back();
  }

  public function cart(Request $request) {
    $cart = $request->session()->get('cart');
    return view('shop/cart', [
      'cart' => (is_array($cart) ? $cart:[]),
    ]);
  }

  public function delete(Request $request) {
    $request->session()->forget('id', 'amount');
    return Redirect()->back();
  }
}

My cart view:

@extends('layouts.app')

@section('content')
@forelse ($cart as $id => $amount)
    <div class="container">
        <li>Product: {{$id}}, Hoeveelheid: {{$amount}}</li>
        <a class="btn btn-danger" href="{{ action('ShopController@delete') }}">Product verwijderen</a>
        <a href="{{ action('ShopController@clear') }}">Winkelwagen legen</a>
    </div>
    {{--{{ dd($cart) }}--}}
@empty
    <div class="container">
        Er is nog niks in uw winkelwagen!
    </div>
@endforelse
@endsection

0 likes
3 replies
lostdreamer_nl's avatar

You'll need to add the $id and change your delete method:

// adding the ID to the link
<a class="btn btn-danger" href="{{ action('ShopController@delete', [$id]) }}">Product verwijderen</a>

// controller
  public function delete(Request $request, $id) {
    // get current cart or empty array
    $cart = $request->session()->get('cart');
    if (!is_array($currentCart)) {
      $currentCart = [];
    }
    // delete product from cart if it exists....
    if (array_key_exists($id, $currentCart)){
      unset($currentCart[$id]);
    }
    // put back the cart data.
    $request->session()->put('cart', $currentCart);
    return Redirect()->back();
  }

And make sure that your delete method is actually a GET request, or the link wont work

tasninta's avatar

@lostdreamer_nl After I click on product delete then I got a 404 page error

I have a get route

Route::get('/delete/{$id}', 'ShopController@delete');
lostdreamer_nl's avatar

What if you do the link like this:

<a class="btn btn-danger" href="/delete/{{ $id }}">Product verwijderen</a>

Do you go to the correct URL?

when running php artisan route:list is the delete route in that list ?

Also: small note before you put these scripts on a linux server:

return Redirect()->back();
// should be
return redirect()->back();

windows is not picky about capitalised letters, linux is.

Please or to participate in this conversation.