[英]Laravel how to return the value NULL when hasMany relationship is empty
在我的laravel
當我的 HasMany 模型返回一個空數組時,我想返回null
值! 我怎樣才能做到這一點?
我試圖這樣做:
public function something(){
$collection = $this->hasMany('App\Models\SomeModel');
return $collection ? $collection : null
}
但這仍然返回
{
something: []
}
但我希望顯示這個:
{
something: null
}
所以,有人可以幫我嗎?
您可以像這樣使用函數count()
:
return count($collection) > 0 ? $collection : null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.