在十月CMS中使用Faker

时间:2018-10-29 04:48:17

标签: octobercms octobercms-plugins octobercms-backend

我在十月CMS中创建服务插件。我有2个转发器字段(包裹,邮递员)。我使用Faker插件在数据库中插入伪数据,但是有错误

为foreach()提供的参数无效

这是我的种子代码

<?php namespace Sepehr\Service\Updates;

use Sepehr\Service\Models\Service;
use Faker;
use October\Rain\Database\Updates\Seeder;

class SeedAllTable extends Seeder
{

    public function run()
    {
        $faker = Faker\Factory::create();
        $service = new Service();

        $service->user_id = 1;
        $service->manager_id = 1;
        $service->sender_postal_code = $faker->postcode();
        $service->sender_address = $faker->address();
        $service->status_id = $faker->numberBetween(1, 6);

        foreach ($service->packages as $package) {
            $package['receiver_postal_code'] = $faker->postcode();
            $package['receiver_address'] = $faker->address();
            $package['weight_id'] = $faker->numberBetween(1, 3);
            $package['post_type_id'] = $faker->numberBetween(1, 3);
            $package['distribution_time_id'] = $faker->numberBetween(1, 3);
            $package['special_services_id'] = $faker->numberBetween(1, 3);
            $package['price'] = 0;
            $package['package_type_id'] = $faker->numberBetween(1, 3);
            $package['insurance_type_id'] = $faker->numberBetween(1, 3);
        }
        $service->save();

1 个答案:

答案 0 :(得分:0)

这不是假货或其他问题。

  

问题是service是新记录,packages第一次为空白,因此模型不知道其定义/模式,因此需要将其分配为数组,然后将其转换为自动json

class User extends Model
{
   // make sure your field is jsonable
   protected $jsonable = ['packages'];
  

现在打包软件包替换

$service->status_id = $faker->numberBetween(1, 6);

foreach ($service->packages as $package) {
    $package['receiver_postal_code'] = $faker->postcode();
    $package['receiver_address'] = $faker->address();
    $package['weight_id'] = $faker->numberBetween(1, 3);
    $package['post_type_id'] = $faker->numberBetween(1, 3);
    $package['distribution_time_id'] = $faker->numberBetween(1, 3);
    $package['special_services_id'] = $faker->numberBetween(1, 3);
    $package['price'] = 0;
    $package['package_type_id'] = $faker->numberBetween(1, 3);
    $package['insurance_type_id'] = $faker->numberBetween(1, 3);
}
$service->save();
  

使用

// ...

$service->status_id = $faker->numberBetween(1, 6);

$packages = [];
$howManyPackges = 3;
while($howManyPackges > 0)
    $package = [];
    $package['receiver_postal_code'] = $faker->postcode();
    $package['receiver_address'] = $faker->address();
    $package['weight_id'] = $faker->numberBetween(1, 3);
    $package['post_type_id'] = $faker->numberBetween(1, 3);
    $package['distribution_time_id'] = $faker->numberBetween(1, 3);
    $package['special_services_id'] = $faker->numberBetween(1, 3);
    $package['price'] = 0;
    $package['package_type_id'] = $faker->numberBetween(1, 3);
    $package['insurance_type_id'] = $faker->numberBetween(1, 3);
    $packages[] = $package;
    $howManyPackges--;
}
$service->packages = $packages;

$service->save();

// ...
  

它将为转发器packages添加3条记录

它应该添加一组软件包,jsonable会将其转换为json

如有疑问,请发表评论。