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

Kaustubh's avatar

How to fetch session array in blade

My controller

class RedirectController extends Controller
{
    public function admin(){

        $test = DB::table('PAGE_TBL')->get();
        Session::put('test', $test);
        return view('admin');
    }
}

My View Blade

<div>
    {{ Session::get('test') }}
</div>

It Display result as

 [{"id":1,"name":"Kaustubh","created_at":null,"updated_at":null}]

But i want to fetch a single value from that array, i tried many things to fetch a particular value but it didnt work.

0 likes
7 replies
Mittensoff's avatar

The following would display test[0]'s id .

  {{ Session::get('test')[0] ['id']}}

If you want all the values displayed, then use a @foreach:

@foreach(Session::get('test') as $test)
{{$test['id']}}
@endforeach
2 likes
Kaustubh's avatar

@Mittensoff it dosent work

It gives an error

FatalErrorException Cannot use object of type stdClass as array

Talky's avatar

If you have just one record in that table you can use first() helper. Just like this:

{{ $test->first()->id }} // this will fetch id of the first record in the collection

If you have plenty, you will have to use Mittensoff's method.

Sumer's avatar

In my case {{ $test->first()->id }} is giving an error

Call to undefined method stdClass::first()

1 like

Please or to participate in this conversation.