2

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
7
  • I think it's already as it is Commented Jul 16, 2019 at 7:25
  • Bro, It's already works with no problem I think there is no need to convert Commented Jul 16, 2019 at 7:25
  • What error are you getting? Commented Jul 16, 2019 at 7:29
  • this is array but i want convert it to object because i want to use it in foreach like {{ $related_docks->used }} in blade Commented Jul 16, 2019 at 7:31
  • 1
    @ArmanBagheri you should be able to access it like {{ $related_docks['used'] }} Commented Jul 16, 2019 at 7:35

1 Answer 1

3

You can do like this, please check

@foreach($related_dock as $key => $related_docks)
  {{ $related_docks[$key]->used }}
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.