Skip to content
Advertisement

How to use isset in if return variable in laravel

i have some condition if request isset or no and retun in view blade laravel like this:

JavaScript

if isset($compare1, $compare) run, $laptop2 not found, any solution for this case…? Thanks before

Advertisement

Answer

The structure you have is incorrect. Currently, your code elseif(isset($compare1, $compare)) will never execute because if either $compare1 or $compare2 are set, your if statement will already exit before it gets to the 3rd one. You also have a lot of redundant code (repeating a line of code depending on which if block is executed) which is easily reduced to fewer lines and cleaner code.

Simple Approach

Consider this; (You should be able to replace your entire if block with this)

JavaScript

Dynamic Approach

This may be a little bit overkill if you are just doing 2 comparisons but it definitely has some upsides.

  • Similar amount of code as the simple approach
  • No need to define separate variables for every comparison (e.g $compare1 = $request->compare1;, etc)
  • Easily add more comparisons to your return by simply adding them to the $comparisons array
  • Future Proof

Code:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement