Robin
Robin

Reputation: 1587

Laravel put one value from the database in a session

I have a issue with the sessions in Laravel... So I want to select some values from the database to show the user image via the session.

This is my LoginController, so when they log in, I give the session defined.

The query that I'm using:

$users_images = DB::table('users')->select('user_image.img')->join('user_image','user_image.uid','=','users.id')->get();

Now, I do hope the query is correct written, I want to select 'img' from the table 'user_image' where the 'uid' in that table is the same as the 'id' from the table 'users'...

Now.... I store the query in a session, the output that I'm having in a var_dump(); is the correct stuff....

So I stored in a session...

Session::put('user_img', $users_images);

Now, that's fine. I request the session in my view.

{{ Session::get('user_img', 'none.png') }}

The full request is this:

<img style="max-width:33px;" src="{{ Config::get('app.url') }}/public/img/user_img/{{ Session::get('user_img', 'none.png') }}"/>

But that doesn't matter, because the {{ Config::get('app.url') }} Always worked.

The error I' receiving is ErrorException (E_UNKNOWN) Array to string conversion But how can I fix this error the best?

Kind regards,

Robin

Upvotes: 0

Views: 1358

Answers (2)

Robin
Robin

Reputation: 1587

I combined both, the single ->first() and the loop, now it does work, thanks alot!

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25435

that's because your session is an array, and you're trying to print it as a string.

Either you loop over it:

@foreach (Session::get('user_img') as $img) 

  <img style="max-width:33px;" src="{{ Config::get('app.url') }}/public/img/user_img/{{ $img }}"/>

@endforeach

Or you just take ONE record:

$users_images = DB::table('users')
->select('user_image.img')
->join('user_image','user_image.uid','=','users.id')
->first();

Upvotes: 1

Related Questions