雄辩的选择领域与关系

时间:2018-05-30 18:54:43

标签: eloquent laravel-5.4

这是控制器

$query = ( new Product() )->where( 'quantity', '>', 0 )
        ->where( 'product_category_id', '!=', null )
        ->where( 'soft_delete', 0 )
        ->whereBetween( 'price', [ $priceFrom, $priceTo ] )
        ->with( [
               'campaign' => function ( $q ) {
               $q->where( 'stock', '>', 0 );
               $q->where( 'expire', '>', Carbon::today()->toDateString() );
        }] )->first()// Check the campaign table if it has any record or not
            ->orderBy( 'price', $sort );

现在我试图从广告系列中获取价格,所以我想在刀片中做的就是这个

@if(count($product->campaign))
   <del>$425.00</del>
   {!! $product->campaign->price !!}
@endif

但我得到了

  

此集合实例上不存在Property [price]。

如何从广告系列中选择价格?

1 个答案:

答案 0 :(得分:0)

    $query = Product::where('quantity', '>', 0)
        ->whereIsNotNull('product_category_id')
        ->where('soft_delete', 0)
        ->whereBetween('price', [$priceFrom, $priceTo])
        ->with([
            'campaign' => function ( $q ) {
                $q->where( 'stock', '>', 0 );
                $q->where( 'expire', '>', Carbon::today()->toDateString());
             }
         ])
         ->first()// Check the campaign table if it has any record or not
         ->orderBy('price', $sort);

此集合实例上不存在Property [price]。此错误,因为您的产品型号具有HasMany广告系列关系。 要获得单独的广告系列价格

@if(count($product->campaign))
   <del>$425.00</del>
   @foreach ($product->campaign as $compaign)
   {!! $coampaign->price !!}
   @endforeach
@endif
相关问题