【问题标题】:laravel 4.0 : Eager loading error - Trying to get property of non-object while selecting conditionlaravel 4.0:急切加载错误 - 尝试在选择条件时获取非对象的属性
【发布时间】:2014-09-29 10:59:09
【问题描述】:

我正在尝试在该类别中显示产品,并且我使用了急切加载。我怎样才能得到它?

我的桌子

  1. 产品 => 产品编号 | productCategory_id |等等
  2. 类别 => 类别 ID |名称_类别
  3. 产品图片 => productImage_id |产品编号 | name_image

产品与类别是一对多的 product 与 productImage 是一对多的

我的产品负责人

$product = M_productImage::with(array('product' => function($query)
            {
                 $query->where('productCategory_id', '!=', 6);

            }))->orderBy('productImage_id','=', 'desc')->groupBy('product_id')->take(4)->get();

我的看法

<ul class="row list-unstyled" style="min-height: 442px;">
                    @foreach($product as $value)
                    <li class="col-xs-12 col-sm-6 col-md-3 col-lg-3 text-center animated">
                        <div class="product">
                            <figure class="figure-hover-overlay">                                                                        
                                <a href="#" class="figure-href"></a>

                                @if($value->product->status_product == 2)
                                <div class="product-new" style="width:100px !important;">Limited Stock</div>
                                @elseif($value->product->status_product == 3)
                                <div class="product-sale" style="width:100px !important;">Sold out</div>
                                @endif
                                <img width="400px" height="450px" class="img-responsive" src="{{ asset('assets/image/product/medium/'.$value->productImage_name)}}" alt="" title="">
                                <span class="bar"></span>
                                <figcaption>
                                    <a href="#" title="Wishlist"><i class="icon-heart"></i></a>
                                    <a href="#" class="shoping"><i class="glyphicon glyphicon-shopping-cart"></i></a>
                                    <a href="#" title="Compare"><i class="glyphicon glyphicon-sort"></i></a>
                                </figcaption>
                            </figure>
                            <div class="product-caption" >
                                <p style="height:50px;"><a href="{{ URL::to('productdetail/'. $value->product->product_id ) }}" class="product-name">{{$value->product->title_product}}</a></p>
                                <p class="product-price"><span>{{$value->product->priceBefore}}</span> {{$value->product->priceAfter}}</p>
                            </div>
                        </div>
                    </li>
                    @endforeach
                </ul>

我正在使用 dd($product) 进行托盘处理,其中有价值并且是真的,但我不知道我的视图有什么问题,视图结果是 尝试获取非对象的属性强>

【问题讨论】:

    标签: laravel-4 eager-loading


    【解决方案1】:

    使用 whereHas

    解决此问题
        $id = 7;
        $product = M_productImage::whereHas('product', function($q) use ($id)
            {
                   $q->where('productCategory_id','!=', $id);
            })->orderBy('productImage_id','=', 'desc')->groupBy('product_id')->take(4)->get();
    

    显示在my pastebin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2010-11-19
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多