在laravel中将数组转换为对象

时间:2019-07-16 07:22:24

标签: php laravel

我正在尝试将php数组转换为对象,我想将其放入刀片中。

这是我创建数组的代码

$related_dock = DB::table('reserve')
            ->join('product_dock', 'reserve.product_id', '=', 'product_dock.product_id')
            ->join('dock', 'product_dock.dock_id', '=', 'dock.id')
            ->select([DB::raw('count(dock_id) as used'), 'dock.dock_name as dock name'])
            ->groupBy('dock_id')
            ->orderBy('used', 'desc')
            ->get();
        return $related_dock;

我的刀片是这个

@foreach($related_dock as $related_docks)
  {{ $related_docks }}
@endforeach

并且此代码返回数组下方

Collection {#1365 ▼
 #items: array:3 [▼
   0 => {#1364 ▼
     +"used": 2
     +"dock name": "Bebek"
   }
   1 => {#1376 ▼
     +"used": 2
     +"dock name": "sisli"
   }
   2 => {#1378 ▼
     +"used": 1
     +"dock name": "Beshiktash"
   }
 ]
}

但是我想将此数组用作这样的对象

@foreach($related_dock as $related_docks)
  {{ $related_docks->used }}
@endforeach

1 个答案:

答案 0 :(得分:2)

您可以这样做,请检查

@foreach($related_dock as $key => $related_docks)
  {{ $related_docks[$key]->used }}
@endforeach