3

I have blade view with @foreach loop. In this blade view I also pass on $user variable in UserController. Inside @foreach I have code {{ $user->getAvatar() }}.

@extends('layouts.app')

@section('content')

@foreach($threads as $thread)
    <div class="media alert {{ $class }}">
        <img src="{{ $user->getAvatar() }}">
        <div class="media-body">
            <p>
            {{ $thread->latestMessage->body }}
            </p>
        </div>    
    </div>
@endforeach

@endsection

This is index() function in my controller

public function index()
    {
        $user = Auth::user();
        $threads = Thread::getAllLatest()->get();
        return view('messenger.index', compact('threads', 'user'));
    }

I have error Undefined variable: user. foreach loop don't see variable from other part of view.

ErrorException (E_ERROR)
Undefined variable: user (View:messenger\partials\thread.blade.php) (View: messenger\partials\thread.blade.php)
Previous exceptions
Undefined variable: user (View: messenger\partials\thread.blade.php) (0)
Undefined variable: user (0)
4
  • please add the full error message Commented Apr 3, 2019 at 12:02
  • You're calling the view messenger.index but the error is coming from messenger\partials\thread.blade.php. Are you showing us all the relevant view code? Are you calling a partial somewhere and not passing that variable to it? Commented Apr 3, 2019 at 12:57
  • Problem is simple. @foreach loop don't see variable from outside. Commented Apr 3, 2019 at 13:09
  • @SergeyS. share full relevant code, surely there is something that missing here Commented Apr 4, 2019 at 4:12

5 Answers 5

2

In your case, please use this code on return

return View::make('messenger.index')

->with(compact('threads'))

->with(compact('user'));

You can use with as many time as you want. This will resolve your issue hopefully.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to check if variable has value or not, if laravel failed to get value of $user->getAvatar() it will definitely return error so just try following

@isset($user->getAvatar())

@endisset

1 Comment

variable have value. if i add {{dd($user)}} in blade template, i see object. But in @foreach loop, this variable is undefined.
0

Try with this:

@foreach($threads as $thread)
    <div class="media alert {{ $class }}">
        <img src="{{ isset($user) ? $user->getAvatar : '' }}">
        <div class="media-body">
            <p>
            {{ $thread->latestMessage->body }}
            </p>
        </div>    
    </div>
@endforeach

Comments

0

Try defining it above foreach loop

@extends('layouts.app')

@section('content')
<?php 
   $user_avatar = $user->getAvatar();
?>
@foreach($threads as $thread)
<div class="media alert {{ $class }}">
    <img src="{{ $user_avatar }}">
    <div class="media-body">
        <p>
        {{ $thread->latestMessage->body }}
        </p>
    </div>    
</div>
@endforeach

@endsection

Hope it Helps !

1 Comment

Undefined variable: user_avatar.
0

Why you don't use auth() helper function instead

<img src="{{ auth()->user()->getAvatar() }}">

This way you don't need to pass it trough the controller.

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.