SaschaK
SaschaK

Reputation: 180

Laravel Resource return true if relationship exists

I am building a pretty nice RestAPI in Laravel including Resources. Now I`m a little bit struggeling with this:

my model is arranged by this function

public function friendship(){
    return $this->hasOne('App\Models\Friendship');
}

In resource I`calling

'friendships' => $this->friendship()->get()

So I`m getting all "friendships". Works. But I just want to know if there is a friendship. How does my model then have to look like?

I tried following but does not work

if($this->hasOne('App\Models\Friendship'){
    return true;
}
return false;

Upvotes: 0

Views: 996

Answers (1)

cednore
cednore

Reputation: 885

You can use the exists method to determine the existence. Consider the following snippet;

`friendship` => $this->friendship()->exists(),

Upvotes: 1

Related Questions