Background: We recommend collecte for Laravel loops, but if we want to break or continue the loop, break or continue will cause an error on the syntactically level. The effect of return in the loop is similar to continue, and return false is similar to break
The test demo is as follows
use Illuminate\Database\Seeder;
class MohuaniSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
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; // Exit the loop, like continue}});$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; // Break all loops, like break}});$this->command->info(444); }}Copy the code
Run the seeder
php artisan db:seed --class=MohuaniSeeder
Copy the code
Results: