Php 拉雷维尔雄辩的关系

Php 拉雷维尔雄辩的关系,php,laravel,eloquent,Php,Laravel,Eloquent,这是我的问题 我有一个叫做Product的模型,它定义了这样一种关系 Product.php public function maintenanceById($maintenanceId){ return $this->belongsToMany(Maintenance::class, 'product_maintenance')->where('maintenance_id', '=', $maintenanceId)->withPivot(['price',

这是我的问题

我有一个叫做Product的模型,它定义了这样一种关系

Product.php

public function maintenanceById($maintenanceId){
        return $this->belongsToMany(Maintenance::class, 'product_maintenance')->where('maintenance_id', '=', $maintenanceId)->withPivot(['price', 'code']);
    }
我使用这种方法的观点是这样的

    @foreach($maintenance as $value)

        @if($product->maintenanceById($value->id)->getResults())
          <th>
           {{$product->maintenanceById($value->id)->getResults()[0]->pivot->price}}
          </th>
       @else
          <th></th>
       @endif

   @endforeach
编辑: 我试过这个,效果很好

public function maintenanceById($maintenanceId){
    return $this->belongsToMany(Maintenance::class, 'product_maintenance')->where('maintenance_id', '=', $maintenanceId)->withPivot(['price', 'code'])->get();
}
然后在视图中

@foreach($maintenance as $value)
   @if(!$product->maintenanceById($value->id)->isEmpty())
      <th>£ {{$product->maintenanceById($value->id)->first()->pivot->price}}</th>
  @else
      <th></th>
  @endif
@endforeach
@foreach($value形式的维护)
@如果(!$product->maintenanceById($value->id)->isEmpty())
{{$product->maintenanceById($value->id)->first()->pivot->price}
@否则
@恩迪夫
@endforeach
但是当我尝试下面的代码时,它不起作用

public function maintenanceById($maintenanceId){
    return $this->belongsToMany(Maintenance::class, 'product_maintenance')->where('maintenance_id', '=', $maintenanceId)->withPivot(['price', 'code'])->first();
}


@foreach($maintenance as $value)
   @if(!$product->maintenanceById($value->id)->isEmpty())
        <th>£ {{$product->maintenanceById($value->id)->pivot->price}}</th>

       @else
           <th></th>
       @endif
   @endforeach
公共函数maintenanceById($maintenanceId){
返回$this->belongTomany(Maintenance::class,'product_Maintenance')->where('Maintenance_id','=','maintenanceId)->withPivot(['price','code'])->first();
}
@foreach($维护为$价值)
@如果(!$product->maintenanceById($value->id)->isEmpty())
英镑{{$product->maintenanceById($value->id)->pivot->price}
@否则
@恩迪夫
@endforeach

您可以使用
->first()
,因为根据函数名判断,它应该只返回一个值。因此,将
..->withPivot(['price','code'])
修改为
..->withPivot(['price','code'])->first()
@Andrew这给了我一个糟糕的方法调用示例有没有办法强制返回一个对象?尝试
->first()
而不使用
->getResults()
您可以使用
->first()
,因为根据函数名判断,它应该只返回一个值。因此,将
..->withPivot(['price','code'])
修改为
..->withPivot(['price','code'])->first()
@Andrew这给了我一个糟糕的方法调用例外有没有办法强制返回一个对象?请先尝试
->first()
,不要
->getResults()
public function maintenanceById($maintenanceId){
    return $this->belongsToMany(Maintenance::class, 'product_maintenance')->where('maintenance_id', '=', $maintenanceId)->withPivot(['price', 'code'])->first();
}


@foreach($maintenance as $value)
   @if(!$product->maintenanceById($value->id)->isEmpty())
        <th>£ {{$product->maintenanceById($value->id)->pivot->price}}</th>

       @else
           <th></th>
       @endif
   @endforeach