phplaravelsessionmollie

Deleting a single product from a session array (Laravel sessions & PHP)


i'm currently working on a webshop that's all about different types of hair colors, but i kinda have an issue right now. I'm working on a button that automatically deletes one product from a session file. For that i'm using a $request->session()->forget('cart'); line.

This is what my controller looks like:

public function index()
{
    // Dit zorgt ervoor dat alle producten in het tabel Product-
    // opgehaald worden en in de variabele $products gezet worden

    $products = Product::all();

    return view('winkelmand', compact('products'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return void
 */
public function create()
{
    return false;
}

/**
 * Store a newly created resource in storage.
 *
 * @param \Illuminate\Http\Request $request
 * @return Application|Factory|View
 */
public function store(Request $request)
{
    $product = Product::find($request->id);
    if (!$product) {
        abort(404);
    }
    $id = $request->id;

    $cart = session()->get('cart');
    // Als het winkelwagendje leeg is, dan is dit het eerste product
    if (!$cart) {
        $cart = [
            $id => [
                "name" => $product->name,
                "description" => $product->description,
                "price" => $product->price,
                "quantity" => 1
            ]
        ];

        session()->put('cart', $cart);

        return view('winkelmand');
    }

    // Als het product al bestaat, dan de quantiteit verhogen met 1
    if (isset($cart[$id])) {
        $cart[$id]['quantity']++;
        session()->put('cart', $cart);

        return view('winkelmand');
    }
    // Als het product niet bestaat dan toevoegen met quantiteit = 1
    $cart[$id] = [
        "name" => $product->name,
        "price" => $product->price,
        "description" => $product->description,
        "quantity" => 1
    ];

    session()->put('cart', $cart);

    return view('winkelmand');
}

/**
 * Display the specified resource.
 *
 * @param Request $request
 * @param int $id
 * @return Application|Factory|View
 */
public function show(Request $request, $id)
{
    return false;
}


/**
 * Show the form for editing the specified resource.
 *
 * @param int $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    return false;
}

/**
 * Update the specified resource in storage.
 *
 * @param \Illuminate\Http\Request $request
 * //     * @param int $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request)
{
    if ($request->id && $request->quantity) {
        $cart = session()->get('cart');

        $cart[$request->id]["quantity"] = $request->quantity;

        session()->put('cart', $cart);
        session()->flash('success', 'Cart updated successfully');
    }
}

/**
 * Remove the specified resource from storage.
 *
 * //     * @param int $id
 * @return string
 */
public function destroy(Request $request)
{
    //Deze functie moet ervoor zorgen dat er door middel van een knop de
    // desbetreffende item verwijderd wordt uit het winkelwagentje.
    // Deze functie werkt alleen nog niet en wordt dus niet gebruikt in de webshop

    $request->session()->forget('cart');
    dd('cart');

    return view('winkelmand');
}

I added a button using a form and method POST to the HTML:

<td>
    <form action="/winkelmand/{{ $id }}" method="POST">
        @csrf
        <input name="_method" type="hidden" value="delete" />
        <input type="submit" value="x" />
    </form>
</td>

So it's supposed to retrieve one specific product from the session (from whatever product button is pressed) and then it's supposed to forget that key from a session.

Anybody who can help me with that?

Ps. Sorry for my vocabulary.


Solution

  • You never shared what the issue is. Is it not working ? Based on your code, I am assuming you are getting perma 404.

    Some tips:

    I assume your problem is that you are using $request->id but you are never sending id. You have id in the URL, but for your code to work, you should do:

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return Application|Factory|View
     */
    public function store(Request $request, $id)
    {
        $product = Product::findOrFail($id);
    

    Remember to replace this part of the code with mine above:

    public function store(Request $request)
    {
        $product = Product::find($request->id);
        if (!$product) {
            abort(404);
        }
        $id = $request->id;
    

    This still looks for the product to exists (no random ids). Then get the cart, find if the id exists in that cart and unset it (delete it), save the cart again, and done.

    public function destroy(Request $request, $id)
    {
        Product::findOrFail($id);
    
        $cart = session()->get('cart');
    
        if (isset($cart[$id])) {
            unset($cart[$id]);
    
            session()->put('cart', $cart);
        }
    
        return view('winkelmand');
    }