Laravel 资源 return 如果存在关系则为真
Laravel Resource return true if relationship exists
我正在 Laravel 中构建一个非常不错的 RestAPI,包括资源。现在我对此有点苦恼:
我的模型就是用这个函数整理的
public function friendship(){
return $this->hasOne('App\Models\Friendship');
}
在我调用的资源中
'friendships' => $this->friendship()->get()
所以我得到了所有的“友谊”。作品。但我只想知道是否有友谊。那么我的模型应该是什么样子的?
我试过跟随但没有用
if($this->hasOne('App\Models\Friendship'){
return true;
}
return false;
可以使用exists
方法判断是否存在。考虑以下片段;
`friendship` => $this->friendship()->exists(),
我正在 Laravel 中构建一个非常不错的 RestAPI,包括资源。现在我对此有点苦恼:
我的模型就是用这个函数整理的
public function friendship(){
return $this->hasOne('App\Models\Friendship');
}
在我调用的资源中
'friendships' => $this->friendship()->get()
所以我得到了所有的“友谊”。作品。但我只想知道是否有友谊。那么我的模型应该是什么样子的?
我试过跟随但没有用
if($this->hasOne('App\Models\Friendship'){
return true;
}
return false;
可以使用exists
方法判断是否存在。考虑以下片段;
`friendship` => $this->friendship()->exists(),