如果内部的foreach在Laravel没有按预期工作

时间:2015-08-30 12:16:29

标签: laravel if-statement

我的if在Laravel中没有按预期显示。

这是在模型中:

    public function getTags()
{
    $tags = [];
    foreach (Config::get('products.tags') as $tag) {
        if ($this->$tag)
            $tags[] = $this->getLabel($tag);
    }
    if ($this->extra_tag)
        $tags[] = $this->extra_tag;

    return $tags;
}

这在视图中:

    @foreach($product->getTags() as $tag)
        @if($this->$tag='bespoke shape')
            <div class="tag-purple">
        @else
            <div class="tag-grey">
        @endif
        {{$tag}}</div>
    @endforeach

我希望看到标签'定制形状'有紫色背景和其他标签有灰色背景。不幸的是,它们都有紫色背景。但是,标签的名称会正确显示。

您对我能做些什么能有所帮助吗?

1 个答案:

答案 0 :(得分:0)

$this->tag='bespoke shape'

这不是您比较的方式,这是您分配值的方式。改为使用两个或三个等号来代替:$tag == 'bespoke shape'

相关问题