Zachary Dale
Zachary Dale

Reputation: 749

Blade if condition in controller function

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 ?

Upvotes: 0

Views: 4208

Answers (7)

killstreet
killstreet

Reputation: 1332

Blade is a template parser, which converts code to PHP behind the scenes. If you want to use some if statement in your controller you just use the standard IF statement.

if($q == $a) {
    #more code here
} else {
    $other code here
} 

As suggested above, it's recommended using a switch statement when using a lot of else statements for your if

switch($q){
    case "yes": 
        return $something;
    break;
    case "no":
        return $somethingelse;
        break;
    case "maybe":
        return $whoknows;
        break;
}

Upvotes: 0

devmyb
devmyb

Reputation: 375

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 code

@if .... @elsif

should be

if .... elseif 

better for multiple if ... else you can use switch 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 as

if( $country == 'country1')
       $banner =  "background-image:url(".asset('images/inside8.jpg') .")";
.......

return $banner;

better to use switch case like as

switch($country)
{
     case "country1":
          $banner =  "background-image:url(".asset('images/inside8.jpg') .")";
          break;
     case ..............
}

Upvotes: 1

jp.palubs
jp.palubs

Reputation: 178

NO, you cant. just use the default if else condition in php.

Upvotes: 0

kalidas
kalidas

Reputation: 1

Please try switch case for above condition.

switch ($country) {
    case "country1":
        echo "background-image:url({{ asset('images/inside8.jpg') }})";
        break;
}

It will return exactly the same results.

Upvotes: 0

Poiz
Poiz

Reputation: 7617

You could build up the style within your controller and then pass the resulting data to your view from that Controller and then render it. This snippet may give you a little insight:

<?php

    public function tourBanner($country){
        $extraData  = ['zero', 'one', 'two'];

        switch($country){
            case 'country1':
                $bgImage    = 'images/inside8.jpg';
                break;
            case 'country2':
                $bgImage    = 'images/inside7.jpg';
                break;
            case 'country3':
                $bgImage    = 'images/inside5.jpg';
                break;
            case 'country4':
                $bgImage    = 'images/inside6.jpg';
                break;
            default:
                $bgImage    = 'images/inside10.jpg';
                break;
        }

        return view("your.blade.view", array(
            "bgImage"    => $bgImage,
            'extraData'  => $extraData,
        ));
    }

Then; in the View, it would be a matter of doing something like this:

    <div class="container" style="background-image:url({{ asset($bgImage) }})">
        <div class="col-md-12">
            Some Content...
        </div>
    </div>

Upvotes: 0

Saumya Rastogi
Saumya Rastogi

Reputation: 13693

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

class Controller {
   function a() {
      if($country == 'country1') {
          $asset_str = asset('images/inside8.jpg');
          $banner = "background-image:url({$asset_str})";
      }
      ....
      return $banner;
   }
}

Hope this helps!

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

In controller you should write like this:

if ($country == 'country1') {
    $a = 'background-image:url('.asset('images/inside8.jpg').')';
} elseif ....

But if you're gonna to build HTML/CSS/JS with PHP, it's terrible idea, do not do that.

Upvotes: 0

Related Questions