Blade if condition in controller function

2019-09-07 06:15发布

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 ?

7条回答
走好不送
2楼-- · 2019-09-07 06:53

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>
查看更多
趁早两清
3楼-- · 2019-09-07 06:55

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!

查看更多
趁早两清
4楼-- · 2019-09-07 06:55

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.

查看更多
何必那么认真
5楼-- · 2019-09-07 07:02

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 ..............
}
查看更多
beautiful°
6楼-- · 2019-09-07 07:02

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.

查看更多
霸刀☆藐视天下
7楼-- · 2019-09-07 07:03

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

查看更多
登录 后发表回答