Laravel-在字符串上调用成员函数save()

我无法将会话中的一系列项目保存到mysql数据库中,并报错:

A = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph A.layout(prog='dot') # neato layout #A.draw('test3.pdf') A.draw('test3.png',args='-Gnodesep=0.01 -Gfont_size=1',prog='dot' )

当我做一个dd(Dump and Die)时,数组显示如下:

"Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
  Call to a member function save() on string"

以下是控制器存储功能:

    "{"1":{"name":"Bajiya","quantity":4,"price":"34.00"},"2":{"name":"Gulha","quantity":2,"price":"3.00"},"3":{"name":"kavaabu","quantity":1,"price":"2.00"}}"

我的数据库中有一个表,其中包含ID和购物车列。我希望将上面的数组保存在购物车列中。

谢谢

duoqing321 回答:Laravel-在字符串上调用成员函数save()

每次您覆盖上一个变量$cart

$cart = new Cart; //put into $cart instance of Cart class
$cart = session()->get('cart'); // overwrite other data to variable $cart
$cart = json_encode($cart); // json_encode converts object/array to json (so at this moment you overwrite string to variable `$cart`
$cart->save(); //you're trying to call function on json)))

请尝试理解,您到底想做什么))

,

使用以下功能。

public function storeCart(Request $request){

    $cart = new Cart();
    $cart->cart = $request->session()->get('cart');
    $cart->save();
    $request->session()->forget('cart');
    return redirect()->route('/');
}
本文链接:https://www.f2er.com/3163054.html

大家都在问