目前,下面的函數可以正常工作,但是,->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