Is it possible to use blade @if conditions in functions of controller. This is my function of PublicController
public function tourBanner($country){
@if( $country == 'country1')
background-image:url({{ asset('images/inside8.jpg') }});
@elseif ( $country == 'country2')
background-image:url({{ asset('images/inside7.jpg') }});
@elseif ( $country == 'country3')
background-image:url({{ asset('images/inside5.jpg') }});
@elseif ( $country == 'country4')
background-image:url({{ asset('images/inside6.jpg') }});
@else
background-image:url({{ asset('images/inside10.jpg') }});
@endif
return banner;
}
And i'm getting this error
Parse error: syntax error, unexpected 'if' (T_IF)
What mistake i have made or we can't use blade if if statements in controller ?
The syntax of Blade
@if
is made for using in blade views not into the php files (as in your case controllers). You should use Blade Syntax in to files ending with extension - filename.blade.php, and in you controller please use normal PHP syntax for if statement.Please see Blade Docs
Hope this helps!
Please try switch case for above condition.
It will return exactly the same results.
Typos within your code that code is meant for blade files and not for your PHP files. Listing issues within your code
Remove
@
from your codeshould be
better for multiple
if ... else
you can useswitch case
You were returning
banner
but it should be$banner
and also you were not assigning any value to$banner
variable. So your code be like asbetter to use
switch case
like asIn controller you should write like this:
But if you're gonna to build HTML/CSS/JS with PHP, it's terrible idea, do not do that.
NO, you cant. just use the default
if else
condition in php.