我无法获得集合中的模型列表。似乎是一个简单的问题,但我无法猜出正确的解决方案。
我有一个包含3个外键的数据透视表:
<form name="f1" novalidate ng-submit="submit()">
<input type="radio" ng-model="CustGender" name="uCustGender" value="Male" required /> Male
<input type="radio" ng-model="CustGender" name="uCustGender" value="Female" required /> Female
<span class="error" ng-show="(f1.uCustGender.$dirty || f1.$submitted) && f1.uCustGender.$error.required">Gender required!</span>
<input id="Submit1" type="submit" value="Submit" class="btn btn-success" />
</form>
private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
if (view.FocusedColumn.FieldName == "<name of check field>" && view.GetRowCellValue(view.FocusedRowHandle, "<other field>").ToString()!="editable value"))
e.Cancel = true;
}
在我的`供应商&#39; model我定义了这个关系:
TABLE leadsupplier_product
在给定的潜在客户模板中,我想循环使用id | lead_id | supplier_id | product_id
4 | 128 | 2048 | 8
3 | 120 | 2048 | 7
4 | 120 | 2048 | 8
循环
所以在$ lead = 120的模板中; 我有这个:
public function products()
{
// ->where('leadsupplier_product.lead_id', $leadid)
return $this->belongsToMany(\App\Models\Product::class, 'leadsupplier_product', 'supplier_id', 'product_id')->withPivot('lead_id','id');
}
它按预期工作(我得到适当数量的模型)
foreach
我得到{{$o->products()->wherePivot('lead_id', $lead->id)->count()}}
为什么?
但是在我的// no `()` after the relation name
{{$o->products->wherePivot('lead_id', $lead->id)->count()}}
循环中,我无法打印出这个集合:
ErrorException in Macroable.php line 74: Method wherePivot does not exist.
现在,如果我用参数
定义关系foreach
帮助!
我只是在循环中添加了一个额外的// no objects printed
@foreach($o->products()->where('leadsupplier_product.lead_id', $lead->id) as $ps)
<a class="btn btn-sm bg-greenish">abc {{$ps->id}} </a>
LbyP:{{$ps->pivot->lead_id}} LS: {{$ps->pivot->id}} L:{{$lead->id}}
@endforeach
// no objects printed
@foreach($o->products()->wherePivot('lead_id', $lead->id) as $ps)
<a class="btn btn-sm bg-greenish">abc {{$ps->id}} </a>
LbyP:{{$ps->pivot->lead_id}} LS: {{$ps->pivot->id}} L:{{$lead->id}}
@endforeach
条件
public function productsThisLead($leadid)
{
return $this->belongsToMany(\App\Models\Product::class, 'leadsupplier_product', 'supplier_id', 'product_id')->withPivot('lead_id','id')->where('leadsupplier_product.lead_id', $leadid);
}
// I get no results in the `foreach` loop
@foreach($o->productsThisLead($lead->id) as $ps)
<a class="btn btn-sm bg-greenish">abc {{$ps->id}} </a>
LbyP:{{$ps->pivot->lead_id}} LS: {{$ps->pivot->id}} L:{{$lead->id}}
@endforeach
答案 0 :(得分:0)
你问题的两个部分......
第一部分..
{{$o->products()->wherePivot('lead_id', $lead->id)->count()}}
and it works as expected (I get the proper number of models)
// no `()` after the relation name
{{$o->products->wherePivot('lead_id', $lead->id)->count()}}
I get ErrorException in Macroable.php line 74: Method wherePivot does not exist. Why?
当您在没有()的情况下调用关系时,它将尝试返回关系值...当您使用()调用它时,它将返回您可以继续构建的查询构建器对象...就像您正在做的那样......
现在,当这样做时,它必须是一个完整的查询构建器声明...不要伪造 - &gt; get()......就像这样......那应该有用......
$o->products()->wherePivot('lead_id', $lead->id)->get()
这将返回您正在寻找的集合......
希望这有帮助