A
A
Alfieros2021-09-17 11:40:34
PHP
Alfieros, 2021-09-17 11:40:34

How to get array from json array?

The problem is... Previously, $items->attributes would return the entire array of options selected by the client. Now fuck it! I updated the modules... Now I can't get their list in the admin panel. $item->associatedModel - swears, in the array it gives data in the basket, but I can not get this data on the back. There's an array within an array!
@foreach($item->associatedModel as $key => $value) works in the checkout,
but in the admin panel I can't get the internal array. From the {{ $value }} series. Getting an array from $item is not a problem, but what's inside is a problem. 6144544acbe60337795573.png

Krch I need to get an array of $item->associatedModel attributes. Attribute_value is JSON in the base. But he always gives me NULL on request. Krch I am very stupid, I can’t understand where ?!)

Stupidity is not a vice!

I do not see the point, but for the needs, the model:

<?php

namespace App\Repositories;

use Cart;
use App\Models\Order;
use App\Models\Product;
use App\Models\OrderItem;
use App\Contracts\OrderContract;

class OrderRepository extends BaseRepository implements OrderContract
{
    public function __construct(Order $model)
    {
        parent::__construct($model);
        $this->model = $model;
    }

    public function storeOrderDetails($params)
    {

        $order = Order::create([
            'order_number'      =>  strtoupper(uniqid()),
            'user_id'           =>  auth()->user()->id,
            'status'            =>  'pending',
            'grand_total'       =>  Cart::getSubTotal(),
            'item_count'        =>  Cart::getTotalQuantity(),
            'payment_status'    =>  0,
            'payment_method'    =>  $params['pay'],
            'discord'           =>  $params['discord'],
            'battle_tag'        =>  $params['battle_tag'],
            'skype'             =>  $params['skype'],
            'email'             =>  $params['email'],
            'notes'             =>  $params['notes'],

        ]);

        if ($order) {

            $items = Cart::getContent();

            foreach ($items as $key => $item)
            {
                // A better way will be to bring the product id with the cart items
                // you can explore the package documentation to send product id with the cart
                $product = Product::where('name', $item->name)->first();

                $orderItem = new OrderItem([
                    'product_id'    =>  $product->id,
                    'quantity'      =>  $item->quantity,
                    'qty_slider'      =>  $item->qty_slider, // Принимает значения слайдера.
                    'price'         =>  $item->getPriceSum(),
                    'attribute_value' =>  $item, // Вот тут фокус. Собирает пачку в JSON, дальше не разобрать. До апдейта работало норм. Сейчас же костыль нужен какой-то..

                ]);

                $order->items()->save($orderItem);
            }
        }

        return $order;
    }

    public function listOrders(string $order = 'id', string $sort = 'desc', array $columns = ['*'])
    {
        return $this->all($columns, $order, $sort);
    }

    public function findOrderByNumber($orderNumber)
    {
        return Order::where('order_number', $orderNumber)->first();
    }
}


Here is the checkout
@foreach(Cart::getContent() as $item)
                    <div class="card_item">
                        <div class="left_tems">
                            <a href="{{ $item->slug }}" class="col_img">
                                <img src="{{ $item->conditions }}" alt="">
                            </a>
                            
                            <div class="col_name">
                                <span class="name">{{ Str::words($item->name,20) }}</span>
                                @foreach($item->associatedModel as $key => $value)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter Slobodyanyuk, 2021-09-20
@PeterLS

Try at random with dumps.

try {
  $item = json_decode('your json');
} catch (\Exception $e) {
  dd($e);
}
dd($item);
dd($item->associatedModel);
dd($item->associatedModel->toArray()); // if collection

Well, there is no other way))

A
Alexander, 2021-09-17
@tomclancys

$item->associatedModel to $item['associatedModel']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question