Exemple #1
0
 public function storeChanges($request)
 {
     $groceryListItems = $this->grocerylistItems;
     $newList = array();
     $names = $request->input('name');
     $qtys = $request->input('qty');
     $actives = $request->input('active');
     $newGroceryList = new Collection();
     $pos = 0;
     for ($i = 0; $i < count($names); $i++) {
         if ($names[$i] != "") {
             $pos++;
             $itemInfo = ItemInfo::firstOrCreate(['name' => ucwords($names[$i])]);
             $newGroceryItem = $this->groceryListItems()->where('item_info_id', '=', $itemInfo->id)->first();
             if (isset($newGroceryItem) && ($key = $groceryListItems->search($newGroceryItem)) !== false) {
                 $groceryListItems->forget($key);
                 $newGroceryItem->qty = $qtys[$i];
                 $newGroceryItem->active = $actives[$i];
                 $newGroceryItem->position = $pos;
                 $newGroceryList->push($newGroceryItem);
                 //$newGroceryItem->save();
                 //$newList[] = $newGroceryItem;
             } else {
                 $newGroceryItem = new GroceryListItem(['qty' => $qtys[$i], 'active' => $actives[$i]]);
                 $newGroceryItem->groceryList()->associate($this);
                 $newGroceryItem->itemInfo()->associate($itemInfo);
                 $newGroceryItem->position = $pos;
                 $newGroceryList->push($newGroceryItem);
                 //$newGroceryItem->save();
                 //$newList[] = $newGroceryItem;
             }
         }
     }
     foreach ($groceryListItems as $delItem) {
         echo '<p>' . "DELETE" . '</p>';
         $delItem->delete();
     }
     foreach ($newGroceryList as $item) {
         $item->save();
         $newList[] = $item;
     }
     return $newList;
 }
 /**
  * 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();
         }
     }
 }