【问题标题】:Retrieve Json value from laravel database从 laravel 数据库中检索 Json 值
【发布时间】:2018-06-21 10:15:36
【问题描述】:

我有一个表,其中有一列(名为 order_data)将数据保存为 Json,现在我想从该列中检索数据,但我得到的只是列数据本身,如下面的示例

"{\"29\":{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":1,\"attributes\":[],\"conditions\":[]}}"

我的问题是:

如何从该 json 列中获取我的数据,例如 Product->title 在我的示例代码或价格等中是effewf

这是我目前的功能:

public function index()
    {
        $orders = Order::orderby('id', 'desc')->paginate(10);
        return view('admin.orders.index', compact('orders'));
    }

PS:我清理了我的函数,因为我尝试过的所有方法都不起作用,所以你有清理函数来帮助我以此为基础。

这是我尝试过的:

orderss = Order::orderby('id', 'desc')->get(); 
$orderss = json_decode($orderss->order_data);

我得到了这个错误:

此集合实例上不存在属性 [order_data]。

【问题讨论】:

  • 有个函数叫json_decode(),把你的数据传给它
  • 我没有运气,如果你有样品请提供它可能有效吗?!
  • 你能展示你的尝试吗?
  • json_decode, json_encode, foreach($orders ....) 等等/我可以在 google 上找到的所有内容 :)
  • $orderss 是一个集合,而不是实际的行。所以你需要遍历它来提取数据。

标签: php json laravel


【解决方案1】:

您应该尝试通过 php json 解码器发送它

 json_decode($data);

【讨论】:

  • orderss = Order::orderby('id', 'desc')->get(); $orderss = json_decode($orderss->order_data); 返回Property [order_data] does not exist on this collection instance.
  • 尝试将其作为数组检索,或者干脆 var_dump
  • 已经是我模型中的数组:protected $casts = [ 'order_data' => 'array', ];
  • 数据看起来是受保护的,受保护的数据只能通过继承来访问。看看你是否可以将演员设置为公开
【解决方案2】:

您需要循环使用您传递给视图的变量,如下所示:

<div class="container">
@foreach ($orders as $order )
    {{ dd(json_decode($order->order_data, true))}}
@endforeach
</div>

【讨论】:

  • array:1 [▼ 29 => array:6 [▼ "id" => 29 "name" => "effewf" "price" => 24524 "quantity" => 1 "attributes" => [] "条件" => [] ] ]
  • 您可以将json_decode($order-&gt;order_data) 部分分配给变量并开始将name 作为普通数组获取
  • 它返回{"29":{"id":29,"name":"effewf","price":24524,"quantity":1,"attributes":[],"conditions":[]}}
  • 嗨,试试这样的$data = json_decode($order-&gt;order_data,true); echo $data['29']['name'],这是为了帮助你了解如何获取数组的name,你可能需要一个for循环来动态获取它跨度>
  • 好的,如果我这样做@php $data = json_decode($order-&gt;order_data) @endphp {!! dd($data) !!} 我得到这个{#869 ▼ +"29": {#870 ▼ +"id": 29 +"name": "effewf" +"price": 24524 +"quantity": 1 +"attributes": [] +"conditions": [] } } 但仍然无法得到['29']['name']
【解决方案3】:

您可以将类型转换为模型中的 JSON 或数组:

class Order extends Model {
  protected $casts =[
    'order_data'=>'json' //'array'
  ];
}

【讨论】:

    【解决方案4】:

    这是您的 JSON 字符串的样子:

    {
        "29": {
            "id": 29,
            "name": "effewf",
            "price": 24524,
            "quantity": 1,
            "attributes": [],
            "conditions": []
        }
    }
    

    您首先必须使用 json_decode 将其解码为标准类,然后使用您的值:

    <div class="container">
    @foreach ($orders as $jsonStr )
        {{--*/ $order = json_decode($jsonStr) /*--}}
    @endforeach
    </div>
    

    请注意,$order 现在包含您的 json,但是您遇到了问题,因为您的第一个元素是数字。除非您提前知道该号码,否则您将很难找回它:

    $number = "29"; // in your case but has to be parsed
    $product = $order->$b;
    

    现在产品变量可以用作对象了:

    $product->id;
    $product->price;
    // etc.
    

    额外:

    我可能不推荐这个,相反你应该重新设计你的 json 字符串。但无论如何,这就是我检索号码的方式。

    $a = json_decode($jsonStr);
    $b = get_object_vars($a);
    $keys = array_keys($c);
    
    foreach($keys as $key){
       // $key is now your key
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2021-01-18
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2015-01-27
      • 2018-11-20
      相关资源
      最近更新 更多