/** * Run the database seeds. * * @return void */ public function run() { $user = \Project4\User::firstOrCreate(['email' => '*****@*****.**']); $user->name = 'Admin'; $user->email = '*****@*****.**'; $user->password = \Hash::make('admin'); $user->save(); $user = \Project4\User::firstOrCreate(['email' => '*****@*****.**']); $user->name = 'Jill'; $user->email = '*****@*****.**'; $user->password = \Hash::make('helloworld'); $user->save(); $user = \Project4\User::firstOrCreate(['email' => '*****@*****.**']); $user->name = 'Jamal'; $user->email = '*****@*****.**'; $user->password = \Hash::make('helloworld'); $user->save(); }
/** * Run the database seeds. * * @return void */ public function run() { # First, create an array of all the users we want to associate comics with # The *key* will be the user name, and the *value* will be an array of comic_ids. $users = ['Admin' => [41530, 58530, 56205], 'Jill' => [41530, 58530, 56205], 'Jamal' => [41530, 58530, 56205]]; # Now loop through the above array, creating a new pivot for each user to comic foreach ($users as $name => $comic_ids) { # First get the user $user = \Project4\User::where('name', 'like', $name)->first(); # Now loop through each comic for this user, adding the pivot foreach ($comic_ids as $comic_id) { $comic = \Project4\Comic::where('comic_id', 'LIKE', $comic_id)->first(); # Connect this comic to this user # (create a many to many entry) $user->comics()->save($comic, array('collection_count' => 1, 'wishlist_count' => 1)); } } }
public static function addComicToUserWishList($id) { // find current user $user = \Project4\User::where('id', '=', \Auth::id())->first(); // first, see if there is already an entry in the ComicUser table for this combination $comics = $user->comics; $exists = false; foreach ($user->comics as $comic) { if ($comic->id == $id) { $exists = true; } } // IF there is, set the collection value to 1 if ($exists) { $user->comics()->updateExistingPivot($id, ['wishlist_count' => 1]); } else { $comic = \Project4\Comic::where('id', 'LIKE', $id)->first(); # Connect this comic to this user # (create a many to many entry) $user->comics()->save($comic, array('collection_count' => 0, 'wishlist_count' => 1)); } }
/** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]); }
// not really the best to use a get here, but kind of had to so that remove could work in an anchor tag Route::get('/wish-list/remove/{id?}', function ($id = null) { // comic for user and set wishlist to 0 $user = \Project4\User::where('id', '=', \Auth::id())->first(); $user->comics()->updateExistingPivot($id, ['wishlist_count' => 0]); // check if collection is also zero and detach if it is $comics = $user->comics; foreach ($comics as $comic) { if ($comic->id == $id && $comic->pivot->collection_count == 0) { $user->comics()->detach($id); } } \Session::flash('message', 'Comic successfully removed from your wish list.'); return redirect('/wish-list'); }); // not really the best to use a get here, but kind of had to so that remove could work in an anchor tag Route::get('/collection/remove/{id?}', function ($id = null) { // comic for user and set collection to 0 $user = \Project4\User::where('id', '=', \Auth::id())->first(); $user->comics()->updateExistingPivot($id, ['collection_count' => 0]); // check if wishlist is also zero and detach if it is $comics = $user->comics; foreach ($comics as $comic) { if ($comic->id == $id && $comic->pivot->wishlist_count == 0) { $user->comics()->detach($id); } } \Session::flash('message', 'Comic successfully removed from your collection.'); return redirect('/collection'); }); });