2017-04-09 56 views
0

控制器:Laravel:鑑於葉片JSON數據

$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> 

錯誤:

如果我只用{!! $計算!!}它的工作原理,但我需要像{!! $ calculation-> material_type1 !!}

回答

1

您正在將您的Json轉換爲Array,那就是您無法像訪問對象一樣訪問您的鍵。所以改變你的這條線

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

這樣,然後嘗試。它會解碼你的json像對象

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