Laravel:视图刀片中的JSON数据

时间:2017-04-09 14:31:02

标签: php json laravel view blade

控制器:

$commission = Commission::findOrFail($id);

    $json_calculation = (array) json_decode($commission->price_calculation, true);

    return view('tradesman.commissions-list-detail')
        ->with('commission', $commission)
        ->with('calculations', $json_calculation);

JSON:

{"price_per_hour":"180","hours":"4","material_type_1":"Something","material_price_1":"1200","material_type_2":"Something","material_price_2":"800"}

查看:

@forelse($calculations as $calculation)
                <p>{!! $calculation->price_per_hour !!}</p>

错误:

ErrorException: Trying to get property of non-object

如果我只使用{!! $计算!!}它有效,但我需要像{!! $ calculation-&gt; material_type1 !!}

1 个答案:

答案 0 :(得分:1)

您正在将Json转换为数组,这样您将无法像对象一样访问您的密钥。所以改变你的这一行

$json_calculation = (array) json_decode($commission->price_calculation, true);
像这样然后试试。它会解码你的json对象

$json_calculation = json_decode($commission->price_calculation);