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

vipin93's avatar

Undefined property in L4.2

I try to show users profile with his/her uploaded images i have three differents table users, profiles and images my vies is http://laravel.io/bin/Nk3aG and my controller http://laravel.io/bin/E3L2E and my relationship http://laravel.io/bin/ro6jM when i try show in browser ist say error Undefined property: Illuminate\Database\Eloquent\Collection::$file (View: C:\lamp\www\flash\app\views\profile\show.blade.php)

help??

0 likes
2 replies
bobbybouwmann's avatar
Level 88

@user->images returns an collection and not a single object

This

@if($user->images)
 
  <div class="col-md-4">
     <a href="{{ asset('images/'.$user->images->file) }}">
          <img src="{{ asset('images/'.$user->images->file) }}" alt="{{ $user->images->title }}">
    </a>
  </div>
  @endif

Should be more like this:

@if($user->images)

    @foreach ($user->images as $image)
    
        <div class="col-md-4">
            <a href="{{ asset('images/' . $image->file) }}">
                <img src="{{ asset('images/' . $image->file) }}" alt="{{ $image->title }}">
            </a>
        </div>

    @endforeach

@endif

Please or to participate in this conversation.