/** * Insert data into the database * * @param array $data Data */ protected function seed($seeds) { $table = $this->getTableName(); $model = $this->getModelName(); // Format data $seeds = Arrays::each($seeds, function ($seed) use($model) { $seed = new $model($seed); return $seed->getAttributes(); }); // Slice for SQLite $slicer = floor(999 / sizeof($seeds[0])); $slices = array_chunk($seeds, $slicer); foreach ($slices as $slice) { DB::table($table)->insert($slice); } // Print number of seeds // $element = String::remove(get_called_class(), 'Seeder'); // print sizeof($seeds).' '.lcfirst($element).' seeded successfully'.PHP_EOL; }
/** * Sort a collection by value, by a closure or by a property * If the sorter is null, the collection is sorted naturally */ public static function sort($collection, $sorter = null, $direction = 'asc') { $collection = (array) $collection; // Get correct PHP constant for direction $direction = strtolower($direction) == 'desc' ? SORT_DESC : SORT_ASC; // Transform all values into their results if ($sorter) { $results = ArraysMethods::each($collection, function ($value) use($sorter) { return is_callable($sorter) ? $sorter($value) : ArraysMethods::get($value, $sorter); }); } else { $results = $collection; } // Sort by the results and replace by original values array_multisort($results, $direction, SORT_REGULAR, $collection); return $collection; }
/** * Replace a value in an array * * @param array $array The array * @param string $replace The string to replace * @param string $with What to replace it with * * @return array */ public static function replaceValue($array, $replace, $with) { return ArraysMethods::each($array, function ($value) use($replace, $with) { return str_replace($replace, $with, $value); }); }