Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

How to make array with => php (Laravel)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need another level of nesting.

$selprod['items'] = array(
    $request->id => array(
          'id' => $request->id,
          'name' => $request->name,
          'code' => $request->code,
          'img' => $request->img,
        )
    );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...