Home > Net >  How to convert an if condition to ternary operator in laravel?
How to convert an if condition to ternary operator in laravel?

Time:10-09

I am very new to the laravel, I am writing a normal if condition I want to convert that if condition to ternary operator ,please help me to convert it

$posts=DB::table('posts')->where('name',$id)->exists();
if($posts == false)
return $user->hasRole() ||$user->hasRights();

CodePudding user response:

The ternary operator is the same as Javascript with

[condition] ? [if true] : [if false]

if you want to refer to this is not Laravel, because Laravel is just a framework, you need to look for PHP because PHP is the language.

CodePudding user response:

I don't use Laravel but I think it's a PHP framework, so, as documented here:

return $posts == false ? $user->hasRole() ||$user->hasRights() : return_val_if_false

You said you don't want to specify a return value if false. In that case you can simply do:

return $posts == false && ($user->hasRole() ||$user->hasRights());

This doesn't use ternary operators though, there is no way to do what you specifically want I think.

  • Related