seb*_*bap 0 query-builder laravel eloquent
换句话说,可以说我有这样的查询:
return Location::valid()
->with('owners', 'gardeners')
->withCount('gardeners')
->get();
Run Code Online (Sandbox Code Playgroud)
返回一些json:
[
{
name: 'LocationA',
nb_gardeners_max: 3,
owners: [...],
garderners: [...],
garderners_count: 2
}
]
Run Code Online (Sandbox Code Playgroud)
在json响应中,我想添加一些自定义条目,例如is_full和available_places:
[
{
name: 'LocationA',
nb_gardeners_max: 3,
owners: [...],
garderners: [...],
garderners_count: 2,
...
is_full: false,
available_places: 1
}
]
Run Code Online (Sandbox Code Playgroud)
我想这与原始/联接/聚合...有关,但我真的不知道该怎么做。任何帮助将不胜感激
编辑
Minhajul的答案确实很有用,但不可能对附加属性执行WHERE子句。
Location::where('is_full',true)->get() // NOT WORKING
Run Code Online (Sandbox Code Playgroud)
尽管仍然可以对它执行filter(),但我想为此使用JOIN来执行单个查询
为此,您可以添加数据库中没有对应列的属性,为此,您需要修改模型
public function getIsFullAttribute()
{
return $this->attributes['name'] == 'value';
}
Run Code Online (Sandbox Code Playgroud)
创建此访问器后,需要在模型中添加它。
protected $appends = ['is_full'];
Run Code Online (Sandbox Code Playgroud)
希望会对您有所帮助。
归档时间: |
|
查看次数: |
760 次 |
最近记录: |