Home > database >  Exception Property [image] does not exist on this collection instance
Exception Property [image] does not exist on this collection instance

Time:09-27

I am following a tutorial to make an instagram clone using Laravel. In the end I started doing some of my own changes.

I am trying to get user's profile title and image from PostsController. Now I can successfully get the profile title but I want to get the same profile's image which I am unable to do. I am using Intervention Image.

This is my PostsController

public function index()
{
    $users = auth()->user()->following()->pluck('profiles.user_id');

    $posts = Post::whereIn('user_id', $users)->latest()->paginate(5);

    $newUsers = Profile::all('id', 'title', 'image')->sortDesc();

    // $newUsersImage = Image::make(public_path("storage/{$newUsers->image}"))->orientate()->fit(100, 100);

    dd($newUsers->image);

    return view('posts.index', compact('posts', 'newUsers'));
}

when I dd newUsers it gives the folling results:

    Illuminate\Database\Eloquent\Collection {#469 ▼
  #items: array:13 [▼
    12 => App\Models\Profile {#1451 ▼
      #fillable: array:4 [▶]
      #connection: "mysql"
      #table: "profiles"
      #primaryKey: "id"
      #keyType: "int"
       incrementing: true
      #with: []
      #withCount: []
       preventsLazyLoading: false
      #perPage: 15
       exists: true
       wasRecentlyCreated: false
      #attributes: array:3 [▶]
      #original: array:3 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
       timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    11 => App\Models\Profile {#1443 ▶}
    10 => App\Models\Profile {#1437 ▶}
    9 => App\Models\Profile {#1454 ▶}
    8 => App\Models\Profile {#1452 ▶}
    7 => App\Models\Profile {#1419 ▶}
    6 => App\Models\Profile {#1442 ▶}
    5 => App\Models\Profile {#1461 ▶}
    4 => App\Models\Profile {#1459 ▶}
    3 => App\Models\Profile {#1433 ▶}
    2 => App\Models\Profile {#1455 ▶}
    1 => App\Models\Profile {#1411 ▶}
    0 => App\Models\Profile {#1410 ▶}
  ]
}

But when I dd $newUsers->image it says: Exception Property [image] does not exist on this collection instance.

But in my view I get the address by using the same $newUsers->image.

Excuse my language and if I couldn't explain better, I am very new to php and laravel.

CodePudding user response:

It's because the return from $newUsers = Profile::all('id', 'title', 'image')->sortDesc(); is an array

You're treating it as a single instance of Profile.

try dd($newUsers[0]->image)

Assuming the model has an image column it should work

CodePudding user response:

From what I see on the code I think that $newUsers is a collection not a single model. Meaning it's a collection of models.

Collection does not have image but each item in the collection has one.

Just do a foreach loop through them and you will get the image of Profile object and model instance.

foreach ($newUsers as $profile) {
  dd($profile->image);
}
  • Related