简体   繁体   中英

How to delete item from session Laravel

how can i delete data from session in laravel. I do not want delete all data by clicking single delete button. I have delete button for each data, so want to delete one by one. my current code is as follows:

public function deleteProduct(Request $request){
    $id = $request->productId;// this id i want to delete which i get from view
    $products = $request->session()->get('product');

    foreach ($products as $key => $value)
    {
        if ($value['id'] == $id)
        {
            unset($products [$key]);
        }
    }

    //then you can redirect or whatever you need
    return redirect()->back();
}

i tried this as well

foreach ($products as $product)
{
    if ($product->id == $id)
    {
        $products->forget($product);
    }
}

My data in session is in below format

[
{
id: 2,
name: "Marlene Reichert",
description: "Debitis asperiores sed sit assumenda unde quo natus. Consequatur est labore tenetur quae. Eius distinctio ea omnis aspernatur porro earum quae.",
category_id: 3,
price: 76,
image: "http://loremflickr.com/400/300?random=71",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
},
{
id: 6,
name: "Kaylin Emard",
description: "Et aperiam omnis nam iure id non fugiat. Excepturi voluptatem ipsam magnam. Esse asperiores ducimus enim et.",
category_id: 8,
price: 14,
image: "http://loremflickr.com/400/300?random=17",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
},
{
id: 5,
name: "Prof. Iliana Mohr",
description: "Autem sequi esse laudantium ut ut explicabo enim. Corporis cupiditate dolorum et ratione sequi architecto. Vitae enim ex hic nihil.",
category_id: 2,
price: 207,
image: "http://loremflickr.com/400/300?random=99",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
}
]

In my network tab, i got following error when i clicked the delete button

{message: "Invalid argument supplied for foreach()", exception: "ErrorException",…}
exception: "ErrorException"
file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php"
line: 33
message: "Invalid argument supplied for foreach()"
trace: [{file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php",…},…]
0: {file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php",…}
1: {function: "deleteProduct", class: "App\Http\Controllers\ProductController", type: "->"}
2: {,…}

on dd()

$id = $request->productId;
    $products = $request->session()->get('product');
    dd($products);

In my network tab i got this

array:1 [
  0 => {#216
    +"id": 1
    +"name": "Keyshawn McDermott Sr."
    +"description": "Error aut quia id dolorem est aut doloribus nesciunt. Quod nihil tenetur ea id voluptas molestias id. Debitis amet dolor est fugiat sed autem."
    +"category_id": 1
    +"price": 59
    +"image": "http://loremflickr.com/400/300?random=36"
    +"created_at": "2019-07-16 10:12:27"
    +"updated_at": "2019-07-16 10:12:27"
    +"qty": 1
  }
]

The $request->session()->forget('product'); is to remove the entire key from session, and you do not want that. So...

how can i delete data from session in laravel.

In your case, you do:

public function deleteProduct(Request $request){
    $id = $request->productId;

    // Get the array from session.
    $products = $request->session()->get('product');

    // Remove unwanted key from array.
    foreach ($products as $key => $value)
    {
        if ($value->id == $id)
        {
            unset($products[$key]);
        }
    }

    // Override the session.
    $request->session()->put('product', $products);

    return redirect()->back();
}

If one day you add the deleteAllProducts() feature in your system, for example, then you're good calling $request->session()->forget('product');

试试这个代码

$request->session()->forget('product');

you can't use foreach for an array instead use the for loop

for($x = 0;$x<sizeOf($products);$x++)
{
  $product = $products[$x];
  #then do your manipulation here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM