0

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 as the image shows below:

enter image description here

Is there anything that I didn't correctly?

2
  • Well, it seems that not all the properties have offers. Is that something you would expect? Commented Sep 16, 2018 at 23:21
  • No, not all properties have offer. Commented Sep 16, 2018 at 23:23

1 Answer 1

1

If not all the properties have offers, then you should check that before the <li>, besides that, offers is a collection, you need to loop through it, that's why you get the error.

@if($properties)
@php ($i = 1)
<ul>
    @foreach($properties as $property)
        @if ($property->offers)
            @foreach ($property->offers as $offer)
                <li>Offer {{ $i++ }}: {{$offer->offer_message}}</li>
            @endforeach
        @endif
    @endforeach
</ul>
@endif

If you want to get only de properties that have offers (Querying Relationship Existence):

$properties = Property::whereUserId(Auth::id())->has('offers')->get();

And you should probably eager load that relationship:

$properties = Property::whereUserId(Auth::id())->has('offers')->with('offers')->get();
Sign up to request clarification or add additional context in comments.

5 Comments

See my edit. "Offers" is a collection, you need to loop through it.
Giiz. I am definetely taking note for this. It works now and I really appreciate the support
Just in addition to that. How can I number them. Say if I have 3 offers, then system should say "Offer 1", "Offer 2" and so on
Glad I could help. If you keep the has('offers'), then you could remove the if ($property->offers). You need to number them within each property? I mean, when you loop through a new property, the count should start from 1 again or not? This could help you: laravel.com/docs/5.5/blade#the-loop-variable
Yes I do need to number them accordingly

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.