/** * Run the database seeds. * * @return void */ public function run() { if ($this->command->confirm('Install default ingredients?')) { $ingredients = explode("\n", file_get_contents(database_path('seeds/lists/ingredients.csv'))); $categories = Term::where('vocabulary_id', '=', \App\Data\Enums\Vocabularies::INGREDIENT_CATEGORIES)->lists('id', 'name')->toArray(); foreach ($ingredients as $ingredientLine) { list($category, $name) = explode('|', $ingredientLine); if (!Ingredient::where('name', $name)->first()) { $ingredient = Ingredient::create(['name' => $name]); $category = data_get($categories, $category); if ($category) { $ingredient->categories()->attach($category); } } } } }