How can I access data via relationship

Posted 2 months ago by Melodia

I have a user that has many properties. This is user should also be able tp view the offers bet on his properties.

So have the relationship set.

User.php

public function properties(){
    return $this->hasMany('App\Property');
}

Property.php

public function offers(){
    return $this->hasMany('App\Offer');
}

Then in my controller this is what I have:

public function my_offers(){
    
    $properties = Property::whereUserId(Auth::id())->get();


    return view('pages.seller.offers.index', compact('properties'));
}

Then I go to my views like this:

@if($properties)
<ul>
    @foreach($properties as $property)

        <li>{{$property->offers->offer_message}}</li>

    @endforeach
</ul>
@endif 

When I view the page I see the below error:

Property [offer_message] does not exist on this collection instance.

But this property exists in my table.

If I change my list item to the one below I can see the array:

 <li>{{$property->offers-}}</li>

I also see that before and after the array with the data, there are two empty arrays.

Is there anything that I didnt correctly?

Hope someone can help

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.