如何在Laravel模型中使用if语句

时间:2018-10-26 03:54:58

标签: laravel model builder

我在模型中使用builder并在刀片中打印数据,

问题是我的数据使用短名称存储在数据库中,因此我想在刀片中打印时使用实际名称,而不仅仅是返回这些短名称,而且我无法获得if语句在我的构建器中正常工作

代码

    public function buildFooter($footer, $positionid = 'footer') 
        { 
          $result = null;
          foreach($footer as $item) 
            if($item->type == $positionid) {
              $result .= "<li class='dd-item nested-list-item' data-position='{$item->position}' data-id='{$item->id}'>
              <div class='dd-handle nested-list-handle'>
                <i class='fas fa-arrows-alt'></i>
              </div>
              <div class='nested-list-content'>

//if part
                if('{$item->widget}' == 'feature-posts')
                    Feature Posts
                elseif('{$item->widget}' == 'feature-posts-sidebar')
                    Feature Posts (Sidebar)
                elseif('{$item->widget}' == 'top-categories-posts')
                    Top Categories (By Posts)
                elseif('{$item->widget}' == 'top-categories-visits')
                    Top Categories (By Visits)
                else
                    Popular Posts
                endif
//if part
                - {$item->status}
                <div class='float-right'>
                  <a href=''>Edit</a> |
                  <a href='#' class='delete_toggle' rel='{$item->id}'>Delete</a>
                </div>
              </div>".$this->buildFooter($footer, $item->id) . "</li>"; 
            }
          return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null; 
        }
        // Getter for the HTML menu builder
        public function getHTMLFooter($items)
        {
            return $this->buildFooter($items);
        }

错误

syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) 

有什么主意吗?

更新

好吧,我发现刀片出现问题,但是我现在刀片加载时我得到了这样的数据:

one

当前代码

public function buildFooter($footer, $positionid = 'footer') 
    { 
      $result = null;
      foreach($footer as $item) 
        if($item->type == $positionid) {
          $result .= "<li class='dd-item nested-list-item' data-position='{$item->position}' data-id='{$item->id}'>
          <div class='dd-handle nested-list-handle'>
            <i class='fas fa-arrows-alt'></i>
          </div>
          <div class='nested-list-content'>
//if part
            if($item->widget == 'feature-posts')
                Feature Posts
            elseif($item->widget == 'feature-posts-sidebar')
                Feature Posts (Sidebar)
            elseif($item->widget == 'top-categories-posts')
                Top Categories (By Posts)
            elseif($item->widget == 'top-categories-visits')
                Top Categories (By Visits)
            else
                Popular Posts
            endif
// if part
            - {$item->status}
            <div class='float-right'>
              <a href=''>Edit</a> |
              <a href='#' class='delete_toggle' rel='{$item->id}'>Delete</a>
            </div>
          </div>".$this->buildFooter($footer, $item->id) . "</li>"; 
        }
      return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null; 
    }
    // Getter for the HTML menu builder
    public function getHTMLFooter($items)
    {
        return $this->buildFooter($items);
    }

更新2

根据评论中的建议,我在下面进行了更改

在返回文本中添加了"..",并在返回文本中添加了''

//code

<div class='nested-list-content'>".
            if ($item->widget == 'feature-posts'){
                'Feature Posts'
            } elseif ($item->widget == 'feature-posts-sidebar') {
                'Feature Posts (Sidebar)'
            } elseif ($item->widget == 'top-categories-posts') {
                'Top Categories (By Posts)'
            } elseif ($item->widget == 'top-categories-visits') {
                'Top Categories (By Visits)'
            } else {
                'Popular Posts'
            }
            - {$item->status}
            ."<div class='float-right'>
//code

现在我明白了

syntax error, unexpected 'if' (T_IF)

打开

if ($item->widget == 'feature-posts'){

2 个答案:

答案 0 :(得分:1)

您错过了一些“和”,请在下面尝试一下,

        public function buildFooter($footer, $positionid = 'footer'){
            $result = null;
            foreach($footer as $item)
                if($item->type == $positionid) {
                    $result .= "<li class='dd-item nested-list-item' data-position='{$item->position}' data-id='{$item->id}'>";
                    $result .= "<div class='dd-handle nested-list-handle'><i class='fas fa-arrows-alt'></i></div><div class='nested-list-content'>";
                    if ($item->widget == 'feature-posts'){
                         $result .= "Feature Posts";
                    } elseif ($item->widget == 'feature-posts-sidebar') {
                         $result .= "Feature Posts (Sidebar)";
                    } elseif ($item->widget == 'top-categories-posts') {
                         $result .= "Top Categories (By Posts)";
                    } elseif ($item->widget == 'top-categories-visits') {
                         $result .= "Top Categories (By Visits)";
                    } else {
                         $result .= "Popular Posts";
                    }
                    $result .="-".$item->status;
                    $result .="<div class='float-right'><a href='#' class='delete_toggle text-danger' rel='{$item->id}'>Delete</a></div></div>".$this->buildFooter($footer, $item->id) . "</li>";
                  }
             return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null;
        }

答案 1 :(得分:1)

你可以试试吗?

我确信还有其他方法可以实现这一目标。您的方法是将视图和控制器操作混合在一起。

<?php 

public function buildFooter($footer, $positionid = 'footer') 
    { 
      $result = null;
      foreach($footer as $item) 
        if($item->type == $positionid) {
          $result .= "
          <li class='dd-item nested-list-item' data-position='"
          . $item->position
          ."' data-id='".$item->id."'>
          <div class='dd-handle nested-list-handle'>
            <i class='fas fa-arrows-alt'></i>
          </div>
          <div class='nested-list-content'>";
//if part
            if($item->widget == 'feature-posts'):
                $result .= "Feature Posts";
            elseif($item->widget == 'feature-posts-sidebar'):
                $result .= "Feature Posts (Sidebar)";
            elseif($item->widget == 'top-categories-posts'):
                $result .= "Top Categories (By Posts)";
            elseif($item->widget == 'top-categories-visits'):
                $result .= "Top Categories (By Visits)";
            else:
                $result .= "Popular Posts";
            endif;
// if part
        $result .= "- " . $item->status
            ."<div class='float-right'>
              <a href=''>Edit</a> |
              <a href='#' class='delete_toggle' rel='{$item->id}'>Delete</a>
            </div>
          </div>".$this->buildFooter($footer, $item->id) . "</li>"; 
        }
      return $result ?  "\n<ol class=\"dd-list\">\n".$result."</ol>\n" : null; 
    }

// Getter for the HTML menu builder
public function getHTMLFooter($items)
{
    return $this->buildFooter($items);
}