【问题标题】:How to delete a specific value in a cookie array Laravel 8如何删除 cookie 数组 Laravel 8 中的特定值
【发布时间】:2022-01-22 21:06:20
【问题描述】:

我正在尝试从名为“愿望清单”的 cookie 数组中删除一个特定值。我尝试使用Cookie::forget('wishlist');。我在控制器中使用了销毁方法。

问题是它删除了 整个 cookie 数组,而不仅仅是指定的值。

销毁()

    public function destroy($id)
    {

        $books = Book::findOrFail($id);

        $cookie = Cookie::get('wishlist');
        $cookieArray = explode(',', json_decode($cookie));
        $flattenedArray = Arr::flatten($cookieArray);

        $arrayId = array_search($id, $flattenedArray);

        unset($flattenedArray[$arrayId]);

        $cookie = Cookie::forget('wishlist');


        return redirect()->route('wishlist.index', ['books' => $books])->withCookie($cookie);
    }

【问题讨论】:

  • 您可以设置一个新的空或空心愿单cookie,因此它也会将其删除。
  • 我只想从 cookie 数组中删除一个特定的值。我想要在数组中没有指定值的相同 cookie
  • 我想你只是用新值重新设置了 cookie

标签: php laravel cookies laravel-8


【解决方案1】:

Cookies 不接受数组,因此您必须将字符串存储在 cookie 中,并在每次使用时爆炸,并在任何更改后内爆以存储在 cookie 中 要解决您的问题,您必须搜索并找到您要查找的值的索引,然后使用 unset() 函数删除该索引。

【讨论】:

    猜你喜欢
    • 2011-12-09
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    相关资源
    最近更新 更多