tomasosho's avatar

ErrorException Trying to get property 'product_id' of non-object (View:

I have my product_id in a foreach loop. My deal relationship is null for some that's why i'm getting the error.

@if($i->deal->product_id != $i->id)

@endif

when i acknowledge the null values, the error disappears, but then the if statement does not work. e.g

@if($i->deal->product_id ?? ' ' != $i->id)

@endif

how can i get the if statement working even with the null values?

0 likes
4 replies
Sergiu17's avatar
@if ( $i->deal && $i->deal->product_id != $id->id )

@endif

like so

1 like
tomasosho's avatar

Yes, but with this only the elseif form is displaying, the if form is not displaying

@if($i->deal && $i->deal->product_id != $i->id)
                                <form action="{{ route('produce.store') }}" style="margin-left : 20px;float:right;"  method="post" data-toggle="tooltip" data-original-title="Add to Deals">
                                    {{ csrf_field() }}
                                    <input type="text" name="deal" value="{{$i->id}}" hidden>
                                    <button type="submit" class="btn btn-success"> Add to Deals</button>
                                </form>

@elseif($i->deal && $i->deal->product_id == $i->id)
                                <form action="{{ route('produce.update', $i->deal->id) }}" style="margin-left : 20px;float:right;"  method="post" data-toggle="tooltip" data-original-title="Add to Deals">
                                    {{ csrf_field() }}
                                    <input type="text" name="deal" value="" hidden>
                                    <button type="submit" class="btn btn-danger"> Remove Deal</button>
                                </form>
@endif
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
@if($i->deal !== null)

	@if($i->deal->product_id != $i->id)
		// do something
	@elseif($i->deal->product_id == $i->id)
		// do something else
	@endif

@endif
1 like
tomasosho's avatar

Thanks, i ran it this way.

@if($i->deal == null)
                                
@elseif($i->deal !== null)

@endif

Please or to participate in this conversation.