【问题标题】:Laravel command - ask always returning true?Laravel 命令 - 询问总是返回 true?
【发布时间】:2016-05-07 05:31:52
【问题描述】:

我有这段代码:

if($this->ask('Is this holiday booked? [y|N]')) {
                    $holiday->booked = true;
                } else {
                    $holiday->booked = false;  
                }

在 Laravel 5.2 命令中,但无论响应如何,它似乎总是返回 true。

我也试过了:

 if($this->ask('Is this holiday booked? [y|N]') === true) {
                        $holiday->booked = true;
                    } else {
                        $holiday->booked = false;  
                    }

但是无论我输入 y 还是 n,这总是将它作为 false 输入数据库。

这无疑是一件很愚蠢的事情,但谁能看出我哪里出错了?

谢谢。

【问题讨论】:

  • $this->ask('Is this holiday booking? [y|N]') 不返回布尔值(因为当您使用三等号检查时,它返回 false)。我认为问题出在“询问”功能的某个地方。

标签: php laravel laravel-5


【解决方案1】:

最终使用:

 if(!$this->confirm('Is this holiday booked? [y|N]'), false) {
                        $holiday->booked = false;
                    } else {
                        $holiday->booked = true;  
                    }

【讨论】:

    【解决方案2】:

    或者试试这个:):

    $input = $this->ask('Is this holiday booked? [y|n]');
    
      if($input == 'y' || $input == 'Y') {
          $holiday->booked = true;
      } 
      elseif($input == 'n' || $input == 'N') {
        $holiday->booked = true;
      }
       else {
        $this->error("wrong input"); 
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 2013-08-06
      • 2017-05-10
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多