John Davidson

php - Exception Property [image] does not exist on this collection instance

0 comments
Message:


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.



My Answer:
>user()->following()->pluck('id');
$posts = Post::whereIn('user_id', $users)->with('user')->latest()->get();

foreach($posts as $post){
$post->user->image = Image::make($post->user->image)->encode('data-url');
}

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

In this code snippet, I am assuming that the 'image' property on the user model is a URL to the image file. I am using Intervention Image to encode the image as a data URL before passing it to the view. This way, you can directly use the image in your HTML template without needing to load it separately.

Make sure you have Intervention Image installed and configured correctly in your Laravel project. You can install it using Composer by running the following command:


composer require intervention/image


Also, make sure to import the Image class at the top of your controller file:


use Intervention\Image\Facades\Image;


With these changes, you should be able to access the user's profile image in your view.

Rate this post

4 of 5 based on 7532 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us