/** * Register the listeners for the subscriber. * * @param \Illuminate\Events\Dispatcher $events * @return void */ public function subscribe($events) { Ingredient::observe('App\\Observers\\IngredientModelObserver'); $events->listen('App\\Events\\Ingredient\\IngredientCreated', 'App\\Listeners\\IngredientEventListener@onIngredientCreated'); $events->listen('App\\Events\\Ingredient\\IngredientUpdated', 'App\\Listeners\\IngredientEventListener@onIngredientUpdated'); $events->listen('App\\Events\\Ingredient\\IngredientDeleted', 'App\\Listeners\\IngredientEventListener@onIngredientDeleted'); }
/** * 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); } } } } }
/** * Sync an ingredient's categories. * * @param Ingredient $ingredient * @param int|array $categories * @return Ingredient */ public function syncCategories(Ingredient $ingredient, $categories) { $categories = is_array($categories) ? $categories : [$categories]; $ingredient->categories()->sync($categories); return $ingredient; }