Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jason100's avatar

Json_decoding array Session vars in Blade?

I pushed an array into my session, such that I now have something like:

Session['offices'][0]['name'] = Barcelona
Session['offices'][1]['name'] = Paris
Session['offices'][2]['name'] = London

And tried to retrieve the 'name' value in my blade template like so:

@foreach(Session::get('offices') as $office)
    {{ $office }}
@endforeach

However, the values are displayed, json-formatted:

[{"name":"Barcelona"}],{"name":"Paris"},{"name":"London"}]

Since it's coming from Session, how can I json_decode this data to display as simply:

Barcelona, Paris, London ?

0 likes
6 replies
colender's avatar

did you need the values in JSON format because you could of just

Session['offices'] = array('Paris','London','Barcelona');
jason100's avatar

@colender You do bring up a good point, as I inherited some of this code that creates the Session: ($arrLocationIds is an array of location_id's)

$locations = Location::whereIn('id', $arrLocationIds)->select('name')->get();
Session::push('offices', $locations);

The resulting structure does look unnecessarily complicated, when all I really do want to store is the names, and be able to retrieve them as simply as possible.
How can i?

colender's avatar

Does Location::whereIn('id', $arrLocationIds)->select('name')->get(); give you an array ?

This is just assuming your getting a collection in return

$locations = Location::whereIn('id', $arrLocationIds)->select('name')->get();
Session::push('offices', $locations);

is inside a look right? if so

$temp = array();
 $locations = Location::whereIn('id', $arrLocationIds)->select('name')->get();
array_push($temp,$locations);
}
Session::push('offices', $temp);

or

$locations = Location::whereIn('id', $arrLocationIds)->select('name')->get();
Session::push('offices', array($locations));
jason100's avatar

@kfirba
{{ json_decode($office)->name }}
Results in a "Trying to get property of non-object" Error

jason100's avatar
$locations = Location::whereIn('id', $arrLocationIds)->select('name')->get();
echo "<pre>";
var_dump($locations);
echo "</pre>";

gives me this huge/complicated structure, that then gets stored in Session by: Session::push('offices', $locations)

object(Illuminate\Database\Eloquent\Collection)#220 (1) {
  ["items":protected]=>
  array(1) {
    [0]=>
    object(App\Location)#221 (23) {
      ["table":protected]=>
      string(9) "locations"
      ["fillable":protected]=>
      array(8) {
        [0]=>
        string(4) "name"
        [1]=>
        string(4) "team"
        [2]=>
        string(7) "address"
        [3]=>
        string(5) "phone"
        [4]=>
        string(5) "email"
        [5]=>
        string(4) "city"
        [6]=>
        string(5) "state"
        [7]=>
        string(3) "zip"
      }
      ["connection":protected]=>
      NULL
      ["primaryKey":protected]=>
      string(2) "id"
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["timestamps"]=>
      bool(true)
      ["attributes":protected]=>
      array(1) {
        ["name"]=>
        string(9) "Paris"
      }
      ["original":protected]=>
      array(1) {
        ["name"]=>
        string(9) "Paris"
      }
      ["relations":protected]=>
      array(0) {
      }
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["appends":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["casts":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["with":protected]=>
      array(0) {
      }
      ["morphClass":protected]=>
      NULL
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
    }
  }
}

Please or to participate in this conversation.