vendredi 23 juin 2017

Laravel eloquent ignore field in collection if empty

So I have relationships with multiple tables:

    $business = Business::where('id', $id)
                    ->with('addresses')
                    ->with('location')
                    ->with('gallery')
                    ->get();

which brings back something like:

[{"id":100,"name":"Live","type":"4","email":"p.woj6@gmail.com","logo":null,"twitter_business":null,"facebook_business":null,"instagram_business":"Woas","image":null,"gallery_id":null,"user_id":3,"api_key":null,"created_at":"2017-06-22 15:13:47","updated_at":"2017-06-22 15:13:47","addresses":[{"id":6,"firstline_address":"96","secondline_address":"Street","town":"","city":"","postcode":"","telephone":"","created_at":"2017-06-22 15:13:47","updated_at":"2017-06-22 15:13:47","pivot":{"business_id":100,"address_id":6}}],"location":{"id":95,"business_id":100,"longitude":-2.054067,"latitude":53.486755,"slug":"Live & Now","created_at":"2017-06-22 15:13:47","updated_at":"2017-06-22 15:13:47"},"gallery":null}]

However, I get an error that image which is null, doesn't exist in the collection. How can I say something like, if $business->image is empty, ignore it.

I tried:

    @if($business->image == "null")
        <img class="images" id="image" alt="no image" src="" />
    @else
        <img class="images" id="image" src="" />
    @endif

But with no success, I am looking for an efficient way because sometimes there is more than 1 field being empty. I would like to point out that data is outputted in a different places on the page i.e.

        @if($business->image == "null")
            <img class="images" id="image" alt="no image" src="" />
        @else
            <img class="images" id="image" src="" />
        @endif
<div id="right" class="map">
    <div id="map"></div>

        <script type="text/javascript" src="http://ift.tt/2sY7hql"></script>      
<!-- Displaying markers for business -->
                            @foreach ($business->location as $marker)
                                <script>
                            var map = new google.maps.Map(document.getElementById('map'), {
                            zoom: 19,
                            center: {lat: , lng: }
                             });

                                var marker = new google.maps.Marker({
                                position: {lat: , lng: },
                                map: map,
                                title: ''
  });

                        </script>
                        @endforeach



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tVCTtX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire