Laravel “拉雷维尔”;未定义的偏移量:4“;使用faker时出现异常,如何删除偏移? 公共函数运行(){ $types=[‘旅行’、‘露营’、‘餐馆’、‘食物’]; 对于($i=0;$inumberBetween($min=2000,$max=2000000); $price=$internet->randomFloat($nbMaxDecimals=2,$min=0,$max=100); $expiration=$date->dateTimeBetween($startDate='now',$endDate='+2年'); $title=$lorem->句子($nbWords=3,$variableNbWords=true); DB::表(‘优惠券’)->插入([ 'id'=>$id, “title”=>$title, “价格”=>美元价格, “类型”=>$types[$i], “到期日”=>$expiration ]); } }

Laravel “拉雷维尔”;未定义的偏移量:4“;使用faker时出现异常,如何删除偏移? 公共函数运行(){ $types=[‘旅行’、‘露营’、‘餐馆’、‘食物’]; 对于($i=0;$inumberBetween($min=2000,$max=2000000); $price=$internet->randomFloat($nbMaxDecimals=2,$min=0,$max=100); $expiration=$date->dateTimeBetween($startDate='now',$endDate='+2年'); $title=$lorem->句子($nbWords=3,$variableNbWords=true); DB::表(‘优惠券’)->插入([ 'id'=>$id, “title”=>$title, “价格”=>美元价格, “类型”=>$types[$i], “到期日”=>$expiration ]); } },laravel,faker,Laravel,Faker,该表更新了4行。 需要您的帮助,请无法理解如何克服偏移限制? 还有其他配置吗 Illumb\Foundation\Bootstrap\HandleExceptions::handleError(“未定义的偏移量:4”,“C:\xampp\htdocs\couponsystem\database\seeds\CouponSeeder.php”) 显然,因为变量类型仅由4个元素组成,当foor循环中的变量i为4时,它将抛出一个错误undefined offset 4。解决这个问题 更改此选项,“ty

该表更新了4行。 需要您的帮助,请无法理解如何克服偏移限制? 还有其他配置吗

Illumb\Foundation\Bootstrap\HandleExceptions::handleError(“未定义的偏移量:4”,“C:\xampp\htdocs\couponsystem\database\seeds\CouponSeeder.php”)


显然,因为变量类型仅由4个元素组成,当foor循环中的变量i为4时,它将抛出一个错误undefined offset 4。解决这个问题

更改此选项,“type”=>$types[$i]


对于“type”=>$types[rand(0,3)]

来说,因为变量类型仅由4个元素组成,当foor循环中的变量i为4时,它将抛出一个未定义偏移量4的错误。解决这个问题

更改此选项,“type”=>$types[$i]


对于“type”=>$types[rand(0,3)]

$types
数组只有4个元素,没有索引
4

用于确保数字从不超过3,并保持从0到3的循环

    public function run(){
    $types = ['Travelling','Camping','Restaurants','Food'];

    for ($i = 0; $i < 50; $i++){

        $faker = Factory::create();
        $internet = new Internet($faker);
        $date = new DateTime($faker);
        $lorem = new Lorem($faker);

        $id = $internet->numberBetween($min = 2000,$max = 2000000);
        $price = $internet->randomFloat($nbMaxDecimals = 2, $min = 0, $max = 100);
        $expiration = $date->dateTimeBetween($startDate = 'now', $endDate = '+2 years');
        $title = $lorem->sentence($nbWords = 3, $variableNbWords = true);

        DB::table('coupon')->insert([
            'id'=>$id,
            'title'=>$title,
            'price'=>$price,
            "type"=>$types[$i],
            'expiration'=>$expiration
        ]);
    }
}

您的
$types
数组只有4个元素,没有索引
4

用于确保数字从不超过3,并保持从0到3的循环

    public function run(){
    $types = ['Travelling','Camping','Restaurants','Food'];

    for ($i = 0; $i < 50; $i++){

        $faker = Factory::create();
        $internet = new Internet($faker);
        $date = new DateTime($faker);
        $lorem = new Lorem($faker);

        $id = $internet->numberBetween($min = 2000,$max = 2000000);
        $price = $internet->randomFloat($nbMaxDecimals = 2, $min = 0, $max = 100);
        $expiration = $date->dateTimeBetween($startDate = 'now', $endDate = '+2 years');
        $title = $lorem->sentence($nbWords = 3, $variableNbWords = true);

        DB::table('coupon')->insert([
            'id'=>$id,
            'title'=>$title,
            'price'=>$price,
            "type"=>$types[$i],
            'expiration'=>$expiration
        ]);
    }
}

$types
中只有4个条目,但是循环了50次,所以一旦索引达到4,它就会中断。
$types
中只有4个条目,但是循环了50次,所以一旦索引达到4,它就会中断。