How to break long array into subarrays returned from functions in PHP?

  laravel, php

I have this PHP function in Laravel (in my database seeder):

DB::table('table_name')->insert([
   [first_record],
   [second_record],
   ...
   [nth_record]
]);

I can’t use faker libraries and I need to specify each record. However, I decided to break this method into smaller methods this way:

public function run() 
{
    DB::table('table_name')->insert([
       $this->firstMethodToReturnPartOfArray(),
       $this->secondMethodToReturnPartOfArray(),
       ...
       $this->nthMethodToReturnPartOfArray()
    ]);
}

public function firstMethodToReturnPartOfArray()
{
    return [
        [first_record],
        [second_record],
        ...
        [nth_record]
    ];
}

public function secondMethodToReturnPartOfArray()
{
    return [
        [first_record],
        [second_record],
        ...
        [nth_record]
    ];
}

But I get this error:

Array to string conversion
at vendor/laravel/framework/src/Illuminate/Support/Str.php:524

I’m new to PHP and Laravel. How should I fix this?

Source: Ask PHP

LEAVE A COMMENT