【问题标题】:Empty returned value空返回值
【发布时间】:2021-07-07 07:12:45
【问题描述】:

我想做一个函数来获取交易代码但返回值为空。

我正在从我的模型中调用此函数。

public function getTransactionCode($type){

    switch($type)
    {
        case '52';
        $count = $this->where('type_id',$type)
                    ->count('id');
        $next = (intval($count) ?: 0) + 1;
        $code = "LAP-00{$next}";
        return $code;

        case '53';
        $count = $this->where('type_id',$type)
                    ->count('id');
        $next = (intval($count) ?: 0) + 1;
        $code = "PC-00{$next}";
        return $code;

        case '54';
        $count = $this->where('type_id',$type)
                    ->count('id');
        $next = (intval($count) ?: 0) + 1;
        $code = "TAB-00{$next}";
        return $code;

        case '55';
        $count = $this->where('type_id',$type)
                    ->count('id');
        $next = (intval($count) ?: 0) + 1;
        $code = "OOE-00{$next}";
        return $code;

    }
  
}

我想在我的控制器上使用这个函数从我的模型中提取任何值。

 $code = $this->equipment->getTransactionCode($type = '');

但它总是返回空值。

【问题讨论】:

    标签: php laravel laravel-4 switch-statement


    【解决方案1】:

    正如@shaedrich 所建议的,switch 不会返回任何内容。但是,您可以为变量赋值,然后在 switch 语句之后返回。

    case 后面还应该有冒号(:),而不是分号(;)

    public function getTransactionCode($type){
        $code = 'ANY DEFAULT VALUE'; // Needs to set any desired value here
    
        switch($type)
        {
            case '52':
            $count = $this->where('type_id',$type)
                        ->count('id');
            $next = (intval($count) ?: 0) + 1;
            $code = "LAP-00{$next}";
            break;
    
            case '53':
            $count = $this->where('type_id',$type)
                        ->count('id');
            $next = (intval($count) ?: 0) + 1;
            $code = "PC-00{$next}";
            break;
    
            case '54':
            $count = $this->where('type_id',$type)
                        ->count('id');
            $next = (intval($count) ?: 0) + 1;
            $code = "TAB-00{$next}";
            break;
    
            case '55':
            $count = $this->where('type_id',$type)
                        ->count('id');
            $next = (intval($count) ?: 0) + 1;
            $code = "OOE-00{$next}";
            break;
        }
        
        return $code;
    }
    

    【讨论】:

      【解决方案2】:

      switch 并不是真正为return 设计的。

      public function getTransactionCode($type){
          $count = $this->where('type_id',$type)
                        ->count('id');
          $next = (intval($count) ?: 0) + 1;
      
          if ($type === '52') {
              return "LAP-00{$next}";
          } else if ($type === '53') {
              return "PC-00{$next}";
          } else if ($type === '54') {
              return "TAB-00{$next}";
          } else if ($type === '55') {
              return "OOE-00{$next}";
          }
        
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-21
        • 2015-11-07
        • 2020-05-20
        • 2020-11-08
        • 2013-10-09
        • 2012-09-25
        • 2012-03-29
        • 2019-11-24
        相关资源
        最近更新 更多