nima
nima

Reputation: 33

How to allow $user variable in a blade view page in Laravel?

I have this page support.blade.php in views folder and I'm using Laravel.

I can't echo out $user->id or $user[id] in this page and I get undefined variable error. It's possible to echo it out in other pages, like in tickets.blade.php but in this specific page it's not working.

How can I fix this?

Upvotes: 3

Views: 1014

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

You need to pass the data first from controller method:

return view('support', ['user' => $user]);

If you want to display ID of an authenticated user, you can do this from any part of the app without passing data from controller method:

{{ auth()->user()->id }}

Upvotes: 1

Meera Tank
Meera Tank

Reputation: 719

Did you pass data in view file??

return view('support', ['user' => $user]);

Upvotes: 0

Ian
Ian

Reputation: 3676

Within your Service Provider you can use

View::share('key', 'value')

You can consult the docs for a more indepth explanation.

Upvotes: 1

Related Questions