0
votes

I create Service Plugin in October CMS. I have 2 repeaters fields (packages, postman). I use Faker Plugin to Insert dummy data in the database but has an error

Invalid argument supplied for foreach()

This is my code for seed

<?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
Hey, repeater fields generally save data in a Jsonable format so you can try to convert an array into JSON and you can save the repeater field data.Chirag Patel
thanks, I try that but service->save() not working I test in another plugin without repeater and service->save() not workingehsan
You are using foreach loop for packages like $service->packages this is not working because you want to create service, $service->packages this will work when already service will be there and It will contain packages.Chirag Patel
@ChiragPatel may you send me an example code?ehsan

1 Answers

0
votes

Its not a problem of faker or anything.

problem is service is new record and packages will be blank at first time, so model do not know its definition/schema so you need to assign it as array, then it will be converted to json automatically

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

Now for the packages Replace

$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();

With

// ...

$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();

// ...

It will add 3 records for repeater packages

it should add array of packages and jsonable will convert it in to json.

if any doubt please comment.