0

Let me show you what I have.

//Post Controller

public function showPost(Post $post)
{
$albums = $post->album()->with('photos')->get();
$comments = $post->comments()->where('approved', '=', 1)->get();
$this->layout->title = $post->title;
$this->layout->main = View::make('home')->nest('content','posts.single', compact('post', 'comments','albums'));
}

mysql sentence executed correctly

string 'select * from albums where albums.id = '{"id":19,"title":"Post no 19","read_more":"Lorem i'... (length=786) string 'select * from albums where albums.post_id = '19'' (length=54) string 'select * from images where images.album_id in ('4')' (length=57)

I want to access the Photo object in blade, everything parses to the template but when I try to get the Photos

@foreach($albums->Photos as $photo)

Undefined property: Illuminate\Database\Eloquent\Collection::$Photos (View:

// post model

public function album()
    {
        return $this->hasOne('Album');

    }

//album model

public function Photos(){

        return $this->hasMany('Image');
}

2 Answers 2

1

Try

@foreach($albums->Photos()->get() as $photo)
{{$photo->attribute}}
@endforeach

You need to call the function that holds the relationship then use

get()

method to return an Eloquent Collection and then iterate it with a foreach loop.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, you can also try to create an accessor for more info go here
0
@foreach($albums as $album)
    @foreach($album->Photos as $photo)
    <p>{{ $photo->image }}</p>
    @endforeach
    @endforeach

THats the answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.