typora-root-url: ../images
Laravel collecte break continue
背景: Laravel框架中循环我们都推荐使用 collecte 进行循环,但是如果我们想要在循环中
break 或者 continue,直接break或者continue,语法层面会直接报错,那么怎么才能实现上述所要的效果呢。其实在循环中 return的效果就类似与break,而 return false 的效果就类似于 continue
测试demo如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   |  <?php
  use Illuminate\Database\Seeder;
  class MohuaniSeeder extends Seeder {     
 
 
 
      public function run()     {         $this->collectContinue();         $this->command->info("--------------");         $this->collectBreak();     }
      public function collectContinue () {         $arrList = [             "a" => "A",             "b" => "B",             "c" => "C",         ];
          $this->command->info(111);         collect($arrList)->each(function ($arr) {             $this->command->info(222);             if ($arr == "b") {                 $this->command->info(333);                 return;              }         });
          $this->command->info(444);     }     public function collectBreak () {         $arrList = [             "a" => "A",             "b" => "B",             "c" => "C",         ];
          $this->command->info(111);         collect($arrList)->each(function ($arr) {             $this->command->info(222);             if ($arr == "b") {                 $this->command->info(333);                 return false;              }         });
          $this->command->info(444);     } }
 
 
 
  | 
 
运行seeder
1
   | php artisan  db:seed  --class=MohuaniSeeder
 
  | 
 
结果:
