I have my shopping cart with main logic in custom class:
public $items = NULL;
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart){
if($oldCart){
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item, $id){
$storedItem = [
'qty' => 0,
'id' => $item->id,
'prod_url' => $item->url,
'code_cat' => $item->category->code,
'url_cat' => $item->category->url,
'name' => $item->name,
'cost' => $item->price,
'price' => $item->price,
'img' => $item->cardImage->path
];
if($this->items){
if(array_key_exists($id, $this->items)){
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['cost'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
To place order I serialize this array and store in DB.
public function new_order_place(Request $request){
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$order = new Order();
$order->cart = serialize($cart);
$order->name = $request->input('name')?$request->input('name'):Auth::user()->name;
$order->email = $request->input('e-mail') ? $request->input('e-mail'):Auth::user()->email;
$order->phone = $request->input('phone')?$request->input('phone'):(Auth::user()->phone?Auth::user()->phone:$this->validate($request, ['phone' => 'required']));
$order->address = $request->input('address');
Auth::check()?Auth::user()->orders()->save($order):$order->save();
Session::forget('cart');
return redirect()->route('index');
}
Array looks like:
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => AppClassesCart
[items] => Array
(
[4] => Array
(
[qty] => 1
[id] => 4
[prod_url] => gorenje_g_5111_wf
[code_cat] => large-home-appliances
[url_cat] => cookers
[name] => Плита газовая GORENJE G 5111 WF
[cost] => 490
[price] => 490
[img] => img_16.jpg
)
)
[totalQty] => 1
[totalPrice] => 490
)
But also I have a modal with a Buy Now button where a user can buy a specific product without adding it to the cart. I am trying to create the same array because I cannot display orders in the user panel due to the different array structure.
public function modal_order_place(Request $request){
$selprod['items'] = array(
$request->id => array(
'name' => $request->name,
'qty' => $request->qty,
'code' => $request->code,
'img' => $request->img,
'totalPrice' => $request->totalPrice
)
);
$order = new Order();
$order->cart = serialize($selprod);
$order->name = $request->username;
$order->email = $request->email;
$order->phone = $request->phone;
Auth::check()?Auth::user()->orders()->save($order):$order->save();
return response()->json([
'notif_text' => 'Your order has been accepted for processing! Expect a call!'
]);
}
Laravel swears:
Attempting to get the "items" property of a non-object
If I delete the product added through the modal window, then everything is already working. The problem is in the array.
How is it possible to create the same array?
question from:
https://stackoverflow.com/questions/65650391/how-to-make-array-with-php-laravel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…