How to Seed Multiple Data

Submitted by Adewagold - 7 years ago

This is the trick i use whenever i want to seed states or countries. I simply put the states inside an array and create a loop through the array and save each record into the database instead of repeating the instance to save the records.

public function run()
    {
    	$states = ["Abia","Adamawa","Anambra","Akwa Ibom","Bauchi","Bayelsa","Benue","Borno","Cross River","Delta","Ebonyi","Enugu","Edo","Ekiti","Gombe","Imo","Jigawa","Kaduna","Kano","Katsina","Kebbi","Kogi","Kwara","Lagos","Nasarawa","Niger","Ogun","Ondo","Osun","Oyo","Plateau","Rivers","Sokoto","Taraba","Yobe","Zamfara","Federal Capital Territory"];

	for ($item=0; $item < count($states); $item++) { 
		foreach ($states as $state) {
			$state = new States;
			$state->description = $states[$item];
			$state->save();
			$item+=1;
			}
		}