Using wherePivot only if $value present ?

Posted 5 months ago by BarryJames

Hi guys

Any help would be tremendously appreciated!

public function users($type = null)
    {
        return ! $type ? $this->belongsToMany(User::class)->withPivot('type') : $this->belongsToMany(User::class)->withPivot('type')->wherePivot('type', $type);
    }

I currently have 2 functions - one where there is no $type and no wherePivot - and another where there is $type and withPivot - which works fine. But then I thought, surely there must be a way to have one function to do both tasks? Perhaps a anonymous function closure or interface?

Thanks!

Please sign in or create an account to participate in this conversation.