Laravel 5 get property from collection in blade

I need to fetch all Images from A Property Model. But all I get is a Collection with Images but I dont know how to access these.

Here is some Code:


public function show($id)
    $property = Property::findOrFail($id);
    $main_image = $property->images()->mainImage()->get();
    $images = $property->images()->get();

    return view('property.single',compact('property', 'images', 'main_image'));

I set up relations between Property and image:

Property Model:

public function images()
    return $this->hasMany('App\Image');

Image Model:

public function property()
    return $this->belongsTo('App\Property');

public function scopeMainImage($query)
    $query->where('main_image', '=', 1);

I thought an {{ $main_image->file_name }} does the job but the print_r returns some collection


[attributes:protected] => Array
                        [id] => 20
                        [property_id] => 25
                        [user_id] => 2
                        [file_name] => main_image.jpg
                        [file_type] => jpg
                        [file_url] => img/properties/25/main_image.jpg
                        [main_image] => 1
                        [created_at] => 2015-12-02 15:31:03
                        [updated_at] => 2015-12-02 15:31:03


So what can I do to change that to access this collection?

Thank you!


When you call get() on Eloquent query builder, you'll always get a collection, even if one record is returned. Call first() instead and you'll get the first (and only - in your case) object in your collection:

$main_image = $property->images()->mainImage()->first();

Need Your Help

Updating Ubuntu packages fails to fetch

ubuntu apt-get

I'm trying to get my ubuntu(14.04LTS) updated, and I failed with this message:

CURL Authentication being lost?

php authentication cookies curl

I am authenticating a login via CURL just fine. I have a variable I am using to display the returned HTML, and it is returning my user control panel as if I am logged in.