Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 450
Poopy Doop Avatar asked Sep 01 '26 02:09

Poopy Doop


1 Answers

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();
like image 55
James Avatar answered Sep 02 '26 18:09

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!