Search code examples
phpif-statementinline

PHP Ternary: Inline if statement


While there is nothing wrong with the following code it's bothering me because I know it could be a simple one line.

if (Auth::user()->id != 1){
    echo User::where('owner', Auth::user()->id)->where('status', 2)->count();
}else {
    echo User::where('status', 2)->count();
}

I am just having problems constructing the statement. If someone could advise please I have tried several variations of:

echo User::(Auth::user()->id != 1 ? where('owner', Auth::user()->id)->)where('status', 2)->count();

Solution

  • Not sure I like ternary for echo (personally), but if you really want to:

    echo Auth::user()->id != 1
        ? User::where('owner', Auth::user()->id)->where('status', 2)->count()
        : User::where('status', 2)->count();