mercredi 2 décembre 2015

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:

PropertyController

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire