php - convert array to object in laravel

33

i'm trying to convert php array to object and i want to get it in my blade.

this is my code that my array is created

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

and my blade is this

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

and this code return below array

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"
   }
 ]
}

but i want use this array as object like this

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

Answer

Solution:

You can do like this, please check

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

People are also looking for solutions to the problem: php - Get env variables names by matching text

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.