public function getHome() { $dates = []; $recipes = Recipe::all(); for ($i = 0; $i < 7; $i++) { array_push($dates, Carbon::now()->startOfWeek()->addDays($i)); } $datas = Day::whereBetween('date', array($dates[0], end($dates)))->get(); foreach ($dates as $key => $date) { if ($datas->whereLoose('date', $date)->first() != '') { $day = $datas->whereLoose('date', $date)->first(); } else { $day = Day::create(['date' => $date]); } $dates[$key] = $day; } $ingredients_list = collect(); foreach ($dates as $day) { foreach ($day->recipes as $recipe) { foreach ($recipe->ingredients as $ingredient) { if ($ingredients_list->where('id', $ingredient->id)->first() && $ingredients_list->where('id', $ingredient->id)->first()->pivot->unit == $ingredient->pivot->unit) { $ingredients_list->where('id', $ingredient->id)->first()->total += $ingredient->pivot->quantity; } else { $ingredients_list->push($ingredient); $ingredients_list->where('id', $ingredient->id)->first()->total = $ingredient->pivot->quantity; } } } } $supply = Auth::user()->ingredients; return view('page.home')->with(['days' => $dates, 'ingredients_list' => $ingredients_list, 'supply' => $supply]); }
/** * Run the database seeds. * * @return void */ public function run() { \App\Day::truncate(); \App\Day::create(['name' => 'Minggu']); \App\Day::create(['name' => 'Senin']); \App\Day::create(['name' => 'Selasa']); \App\Day::create(['name' => 'Rabu']); \App\Day::create(['name' => 'Kamis']); \App\Day::create(['name' => 'Jumat']); \App\Day::create(['name' => 'Sabtu']); }
public function generate() { $dates = []; $recipes = Recipe::all(); for ($i = 0; $i < 7; $i++) { array_push($dates, Carbon::now()->startOfWeek()->addDays($i)); } $datas = Day::whereBetween('date', array($dates[0], end($dates)))->get(); foreach ($dates as $date) { if ($datas->whereLoose('date', $date)->first() != '') { $day = $datas->whereLoose('date', $date)->first(); $day->recipes()->detach(); $day->recipes()->attach($recipes->random()->id); } else { $day = Day::create(['date' => $date]); $day->recipes()->attach($recipes->random()->id); } } return redirect('/'); }