Support the ongoing development of Laravel.io →
Eloquent Packages
Last updated 2 years ago.
0

You can use a database seeder. Here is the example I use:

<?php

use App\Category;
use Illuminate\Database\Seeder;

class CategoriesTableSeeder extends Seeder
{
	public function run()
	{
		// Helper function to populate model attributes
		$node = function () {
			$args = implode(' ', func_get_args());

			return ['name' => "Category $args"];
		};

		// Create first level nodes
		foreach(range('A', 'F') as $letter)
		{
			$node0 = Category::create($node($letter));

			// Create second level nodes
			foreach(range(1, 3) as $number)
			{
				$node1 = $node0->children()->create($node($letter, $number));

				// Create third level nodes
				foreach(['Δ', 'Σ', 'Ω'] as $greek)
				{
					$node2 = $node1->children()->create($node($letter, $number, $greek));
				}
			}
		}
	}
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

George george Joined 10 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.