/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { if (!Auth::check()) { return view('grocery_list', ['user' => 0, 'items' => new Collection()]); } $user = Auth::user(); $list = GroceryList::firstOrCreate(['user_id' => $user->id]); $items = $list->groceryListItems()->orderBy('position')->get(); //$groceryList = $groceryLists(0); return view('grocery_list', ['user' => 1, 'list' => $list, 'items' => $items]); }
/** * Run the database seeds. * * @return void */ public function run() { //find user $users = App\User::all(); //make list //connect list with use foreach ($users as $user) { $list = new GroceryList(['name' => "My Shopping List"]); $list->user()->associate($user); //$user->save(); $list->save(); //$user->groceryLists()->save($list); $itemInfos = App\ItemInfo::all(); //create grocery list items associated with $list $pos = 0; foreach ($itemInfos as $itemInfo) { $pos++; $g = new GroceryListItem(['active' => true, 'qty' => 1, 'position' => $pos]); $g->groceryList()->associate($list); $g->itemInfo()->associate($itemInfo); $g->save(); } } }