【问题标题】:Laravel Display Array on blade with "unserialize"带有“反序列化”的刀片上的 Laravel 显示阵列
【发布时间】:2021-04-12 16:35:28
【问题描述】:

我面临一个问题,即从我的数据库中检索一个数组并将其显示在我的刀片上。

所以为了了解我的工作,我实际上是通过序列化 Products

在我的“Orders Table”中保存“Cart”信息>
public function store(Request $request)
    {

        $order = new Order();
        $order->user_id = request('user_id');
        $order->wilaya = request('wilaya');
        $order->province = request('province');
        $order->address = request('address');
        $order->phone = request('phone');
        $order->quantity = request('quantity');
        $order->subtotal = request('total');
        $order->status = request('status');

        $products = [];
        $i = 0;

        foreach (Cart::content() as $product){
            $products['product_' . $i][]= $product->name;
            $products['product_' . $i][]= $product->options->color;
            $products['product_' . $i][]= $product->price;
            $products['product_' . $i][]= $product->options->size;
            $products['product_' . $i][]= $product->options->image;
            $products['product_' . $i][]= $product->qty;
            $i++;
        }


        $order->products = serialize($products);
        //dd($order);
        if(Cart::count() <= 0){
            return redirect('/products');
        }else{
        $order->save();
        Cart::destroy();
        return view('checkout.thankyou');
        }

    }

因此,当我尝试在我的节目资源中使用 Unserialize"Products" 列中检索我的数据时

public function show($id)
    {
        $orderShow = Order::find($id);
        $arr = unserialize($orderShow->products);
        dd($arr);
        return view('dashboard.order.show', compact('orderShow', 'arr'));
    }

我得到了这个结果

array:2 [▼
  "product_0" => array:6 [▼
    0 => "T-Shirt Fashion 3"
    1 => "black"
    2 => 1300.0
    3 => "S"
    4 => "QFh840we7wZBKO790A6wFlipJB1ASaCpt3zJhJl8.png"
    5 => 1
  ]
  "product_1" => array:6 [▼
    0 => "T-Shirt Fashion"
    1 => "green"
    2 => 1200.0
    3 => "L"
    4 => "CdMVwEJcMQbJjMp70UhRnkNQnRmWxzAGiWKxFeWN.png"
    5 => 1
  ]
]

result of dd($arr) with image

我也试试这个

$json = json_encode(unserialize($orderShow->products));

我得到了这个结果

"{"product_0":["T-Shirt Fashion 3","black",1300,"S","QFh840we7wZBKO790A6wFlipJB1ASaCpt3zJhJl8.png",1],"product_1":["T-Shirt Fashion","green",1200,"L","CdMVwEJcMQbJjMp70UhRnkNQnRmWxzAGiWKxFeWN.png",1]} ◀"

所以我不知道如何处理刀片上的这些数组,我知道我需要一个 Foreach 循环才能以正确的方式显示它,但我不知道如何继续。

【问题讨论】:

    标签: php arrays json laravel


    【解决方案1】:

    $products 存储为对象(带有键和值)而不是纯数组可能是个好主意。

    在您的store 函数中,您可以尝试以下操作:

    $products = [];
    foreach (Cart::content() as $product) {
        // similar to array_push()
        $products[] = [
            "name" => $product->name,
            "color" => $product->options->color,
            "price" => $product->price,
            "size" => $product->options->size,
            "image" => $product->options->image,
            "qty" => $product->qty,
         ];
    }
    

    在您的 show 函数中

    public function show($id)
    {
        $orderShow = Order::find($id);
        $products = unserialize($orderShow->products);
        return view('dashboard.order.show', compact('orderShow', 'products'));
    }
    

    然后,在您的刀片模板中,如果您需要将$products 显示为表格:

    <table>
        <thead>
            <tr>
                <th>name</th>
                <th>color</th>
                <th>price</th>
                <th>size</th>
                <th>image</th>
                <th>qty</th>
            </tr>
        </thead>
        <tbody>
            @foreach($products as $product)
            <tr>
                <td>{{$product['name']}}</td>
                <td>{{$product['color']}}</td>
                <td>{{$product['price']}}</td>
                <td>{{$product['size']}}</td>
                <td>{{$product['image']}}</td>
                <td>{{$product['qty']}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    

    快速提示,在模型而不是控制器中定义属性(反)序列化是一种很好的做法。 查看Eloquent Mutator,并使用以下内容更新您的Order.php 模型:

    class Order extends Model {
      // ...
    
      public function setProductsAttribute($products)
      {
        $this->attributes['products'] = serialize($products);
      }
    
      public function getProductsAttribute()
      {
        return unserialize($this->attributes['products']);
      }
    
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 2023-03-11
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      相关资源
      最近更新 更多