Reputation: 601
I have a function in controller to remove category and its image file. But i am not able to access the path property. I am getting this error Undefined property: Illuminate\Database\Eloquent\Collection::$path. It is returning path but i am unable to use it.
public function remove($id) {
//$category = Category::find($id)->delete();
$category_image = CategoryImage::where('category_id', '=', $id)->get(['path']);
echo $category_image->path;
//return back();
}
Upvotes: 3
Views: 3338
Reputation: 163748
You can use first()
if you need to get just one object:
$category_image = CategoryImage::where('category_id', '=', $id)->first();
if (!is_null($category_image)) { // Always check if object exists.
echo $category_image->path;
}
When you're using get()
, you're getting a collection. In this case you can iterate over the collection and get data from each object, or just use index:
$category_image[0]->path;
Upvotes: 4
Reputation: 142
You get a collection, you have to loop throug the collection this way:
foreach ($category_image as $image) {
echo $image->path;
}
Upvotes: 1