目前,下面的函数可以正常工作,但是,->has('alerts')
会返回包含所有关系数据的整个数组alerts
。我想只返回来自该关系的某些列。Laravel - 仅返回某个具有函数的列
public function getMatches()
{
$matches = Criteria::select('id')
->has('alerts')
->with(['alerts.location' => function($w){
$w->select('id');
}])
->with('alerts.user.companies')
->where('user_id', Auth::id())
->get();
return Response::json(array(
'matches' => $matches,
'status' => '200'
)
);
}
,我想返回的列可以刀片格式内访问(注意,也用于枢轴):
@foreach($matches as $match)
@foreach($match->alerts as $alert)
{{$alert->pivot->criteria_id}}
{{$alert->pivot->alert_id}}
{{$alert->price_value}}
{{$alert->location->type}}
{{$alert->category}}
{{$alert->description}}
{{$alert->category}}
{{$alert->user->companies->first()->logo->url('thumb')}}
{{$alert->pivot->viewed}}
@endforeach
@endforeach
我曾尝试以下:
public function getMatches()
{
$matches = Criteria::select('id')
->has(['alerts' => function ($q){
$q->select('id', 'pivot.criteria_id');
}])
->with(['alerts.location' => function($w){
$w->select('id');
}])
->with('alerts.user.companies')
->where('user_id', Auth::id())
->get();
}
但是,我收到以下错误:
strpos() expects parameter 1 to be string, array given
的错误时,下列功能被添加到has()
功能发生:
->has(['alerts' => function ($q){
$q->select('id', 'pivot.criteria_id');
}])
任何帮助,我怎么可以选择说,从“警报”表中的字段和相应的关系,我会高度欣赏。
'strpos()'在Laravel的' - > has()'函数内被调用吗?或者你把它叫到某个地方?我只问,因为我没有看到你在任何地方叫它。 – 2015-02-09 19:19:02
只要我把'function'加入'has()',就会发生错误,所以我很确信这是错误发生的地方。 – Ben 2015-02-09 19:20:07