如果空的话,Laravel eloquent忽略集合中的字段

时间:2017-06-23 08:44:24

标签: laravel laravel-5 eloquent laravel-5.4 laravel-eloquent

所以我与多个表有关系:

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

带来了类似的东西:

[{"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}]

但是,我收到的错误是该图像为null,在集合中不存在。我怎么能这样说,如果$ business->图像为空,则忽略它。

我试过了:

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

但是没有成功,我正在寻找一种有效的方法,因为有时候有超过1个字段是空的。我想指出的是,数据是在页面的不同位置输出的,即

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

        <script type="text/javascript" src="https://maps.google.com/maps/api/js?v=3&key=AIzaSyAirYgs4Xnt9QabG9v56jsIcCNfNZazq50&language=en"></script>      
<!-- Displaying markers for business -->
                            @foreach ($business->location as $marker)
                                <script>
                            var map = new google.maps.Map(document.getElementById('map'), {
                            zoom: 19,
                            center: {lat: {{$marker->latitude}}, lng: {{$marker->longitude}}}
                             });

                                var marker = new google.maps.Marker({
                                position: {lat: {{$marker->latitude}}, lng: {{$marker->longitude}}},
                                map: map,
                                title: '{{$business->name}}'
  });

                        </script>
                        @endforeach

1 个答案:

答案 0 :(得分:0)

下面:

Business::where('id', $id)
                ->with('addresses')
                ->with('location')
                ->with('gallery')
                ->get()
                ->reject(function($business){
                   return !$business->image;
                });

这将拒绝那些有空图像的商家,如果你仍然希望通过你可以做的图像,但填充你可以尝试的字段:

Business::where('id', $id)
                ->with('addresses')
                ->with('location')
                ->with('gallery')
                ->get()
                ->map(function($business){
                   return $business->image = !$business->image ? $business->image = 'N/A' : $business->image 
                });

或者,您可以检查循环中它是否为空。这里有很多选择^^

还使用:https://laravel.com/docs/5.4/eloquent-mutators

您可以执行以下操作:

这将确保始终存在邮政编码,无论是实际的邮政编码还是0。

public function setPostcodeAttribute($value){
   $this->postcode = !$value ? 0 : $value;
}