Laravel - 仅返回 has 函数中的某些列
Laravel - returning only certain columns within a has function
目前,下面的函数可以正常工作,但是,->has('alerts')
return 是 alerts
的整个数组,包括所有关系数据。我只想 return 该关系中的某些列。
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'
)
);
}
我想要 return 的列可以在 blade 格式中访问 (注意也使用了主元):
@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');
}])
任何关于我如何 select 表示来自 'alerts' table 和相应关系的字段的帮助,我将不胜感激。
在你的代码中你有这样的东西:
->has(['alerts' => function ($q){
//...
}])
应该是这样的:
->whereHas('alerts', function ($q){
//...
})
您不想为此使用 has
或 whereHas
。 has
函数仅用于根据关系过滤结果。仅 select 某些列使用 with()
$matches = Criteria::select('id')
->has('alerts')
->with(['alerts' => function($q){
$q->select('id', 'pivot.criteria_id');
}, 'alerts.location' => function($w){
$w->select('id');
}])
->with('alerts.user.companies')
->where('user_id', Auth::id())
->get();
目前,下面的函数可以正常工作,但是,->has('alerts')
return 是 alerts
的整个数组,包括所有关系数据。我只想 return 该关系中的某些列。
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'
)
);
}
我想要 return 的列可以在 blade 格式中访问 (注意也使用了主元):
@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');
}])
任何关于我如何 select 表示来自 'alerts' table 和相应关系的字段的帮助,我将不胜感激。
在你的代码中你有这样的东西:
->has(['alerts' => function ($q){
//...
}])
应该是这样的:
->whereHas('alerts', function ($q){
//...
})
您不想为此使用 has
或 whereHas
。 has
函数仅用于根据关系过滤结果。仅 select 某些列使用 with()
$matches = Criteria::select('id')
->has('alerts')
->with(['alerts' => function($q){
$q->select('id', 'pivot.criteria_id');
}, 'alerts.location' => function($w){
$w->select('id');
}])
->with('alerts.user.companies')
->where('user_id', Auth::id())
->get();