【问题标题】:How to make array with => php (Laravel)如何使用 => php (Laravel) 制作数组
【发布时间】:2021-01-10 05:32:25
【问题描述】:

我的购物车带有自定义类中的主要逻辑:

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;
    }

为了下订单,我序列化这个数组并存储在数据库中。

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');
  }

数组看起来像:

__PHP_Incomplete_Class Object
(
    [__PHP_Incomplete_Class_Name] => App\Classes\Cart
    [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
)

但我也有一个带有“立即购买”按钮的模式,用户可以在其中购买特定产品而无需将其添加到购物车中。我正在尝试创建相同的数组,因为由于数组结构不同,我无法在用户面板中显示订单。

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 发誓: Attempting to get the "items" property of a non-object 如果我删除通过模态窗口添加的产品,则一切正常。问题出在数组中。

如何创建相同的数组?

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    您需要另一层嵌套。

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

    【讨论】:

    • 非常感谢您的回答,我把问题澄清了一点,您能告诉我该怎么做吗?
    • 在编辑中,您有一个由 API 创建的对象。您需要使用该 API 来创建新对象。
    【解决方案2】:

    找到了解决办法。要解决此问题,您需要在序列化我们的数组之前将其转换为对象。从我的代码:

    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
            )
          );
    
        $object = (object)$selprod;//This line will convert our array to an object
    
        $order = new Order();
    
        $order->cart = serialize($object);//Here we are already serializing our object, not an array
    
        $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 不再发誓:

    stdClass Object
    (
        [items] => Array
            (
                [3] => Array
                    (
                        [name] => name
                        [qty] => 1
                        [img] => img_10.jpg
                        [cost] => 380
                    )
    
            )
    
    )
    

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 2016-08-18
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2014-01-25
      • 2012-08-27
      相关资源
      最近更新 更多