/** * Run the database seeds. * * @return void */ public function run() { $kid_id = \P4\Kid::where('name', '=', 'Jasper')->pluck('id'); DB::table('photos')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'title' => 'Easy Jasper in basket', 'image_url' => 'http://d3drsuq3xbnvq3.cloudfront.net/jp/pictures/201512/537083047/736D1327C4744FADBA97B2438DBCC167.jpg', 'kid_id' => $kid_id]); DB::table('photos')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'title' => 'Happy Jasper on train', 'image_url' => 'http://d3drsuq3xbnvq3.cloudfront.net/jp/pictures/201512/537083047/778FD37C1A554E73AF0824971024E6D6.jpg', 'kid_id' => $kid_id]); DB::table('photos')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'title' => 'Dizzy Jasper playing on toy piano', 'image_url' => 'http://d3drsuq3xbnvq3.cloudfront.net/jp/pictures/201512/537083429/5be3818bf638483a82d49c8cb2d17eb0.jpg', 'kid_id' => $kid_id]); $kid_id = \P4\Kid::where('name', '=', 'John')->pluck('id'); DB::table('photos')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'title' => 'Busy Panda John', 'image_url' => 'http://www.bajiroo.com/wp-content/uploads/2013/01/Funny-Baby-kids-child-images-fun-bajiroo-photos-14.jpg', 'kid_id' => $kid_id]); }
/** * Run the database seeds. * * @return void */ public function run() { $kids = ['John' => ['Jill', 'Jamal', 'Jean'], 'Jasper' => ['Jean'], 'Olivia' => ['Jill', 'Jean'], 'Emily' => ['Jamal', 'Jean']]; # Now loop through the above array, creating a new pivot for each kid corresponding to user foreach ($kids as $name => $users) { # First get the kid name matched $kid = \P4\Kid::where('name', 'like', $name)->first(); # Loop through all users for each kid foreach ($users as $userName) { $user = \P4\User::where('name', 'like', $userName)->first(); # Save the kid's users info $kid->users()->save($user); } } }
/** * Run the database seeds. * * @return void */ public function run() { $kid = \p4\Kid::firstOrCreate(['id' => 20]); $kid->firstname = 'Juluis'; $kid->lastname = 'Go'; $kid->birthday = 2010 - 05 - 07; $kid->picture = 'images/kid4.jpg'; $kid->save(); $kid = \p4\Kid::firstOrCreate(['id' => 21]); $kid->firstname = 'Pah'; $kid->lastname = 'Lim'; $kid->birthday = 2010 - 00 - 07; $kid->picture = 'images/kid4.jpg'; $kid->save(); $kid = \p4\Kid::firstOrCreate(['id' => 22]); $kid->firstname = 'Jus'; $kid->lastname = 'Stry'; $kid->birthday = 2011 - 05 - 10; $kid->picture = 'images/kid4.jpg'; $kid->save(); $kid = \p4\Kid::firstOrCreate(['id' => 23]); $kid->firstname = 'Mary'; $kid->lastname = 'Store'; $kid->birthday = 2012 - 05 - 07; $kid->picture = 'images/kid4.jpg'; $kid->save(); $kid = \p4\Kid::firstOrCreate(['id' => 24]); $kid->firstname = 'Juluis'; $kid->lastname = 'Go'; $kid->birthday = 2012 - 05 - 07; $kid->picture = 'images/kid4.jpg'; $kid->save(); $kid = \p4\Kid::firstOrCreate(['id' => 25]); $kid->firstname = 'Brett'; $kid->lastname = 'Lunz'; $kid->birthday = 2013 - 05 - 07; $kid->picture = 'images/kid4.jpg'; $kid->save(); $faker = \Faker\Factory::create(); foreach (range(1, 15) as $index) { Kid::create(['firstname' => $faker->firstName(), 'lastname' => $faker->lastName(), 'birthday' => $faker->dateTimeThisDecade->format('Y-m-d'), 'picture' => 'images/kid4.jpg', 'created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString()]); } }
/** * Run the database seeds. * * @return void */ public function run() { # First, create an array of all the users we want to associate kids with # The *key* will be the user email, and the *value* will be an array of tags. $users = ['*****@*****.**' => ['Julius', 'Pah'], '*****@*****.**' => ['Brett', 'Mary', 'Jus']]; # Now loop through the above array, creating a new pivot for each user to kid foreach ($users as $email => $kids) { # First get the user $user = \p4\User::where('email', 'like', $email)->first(); echo nl2br("user " . $user . "\n"); # Now loop through each kid for this book, adding the pivot foreach ($kids as $firstname) { echo nl2br("firstname " . $firstname . "\n"); $kid = \p4\Kid::where('firstname', 'LIKE', $firstname)->first(); # Connect this user to this kid $user->kids()->attach($kid); } } }
/** * Responds to requests to Process /photos/delete/{?id} */ public function getDoDelete($kid_id) { $kid = \P4\Kid::with('photos', 'users')->find($kid_id); if (is_null($kid)) { \Session::flash('flash_message', 'Kid not found.'); return redirect('\\kids'); } if ($kid->users()) { $kid->users()->detach(); } //delete photos before deleting kid record if ($kid->photos()) { //delete photo tags related records \DB::table('photo_tag')->join('photos', 'photos.id', '=', 'photo_tag.photo_id')->where('photos.kid_id', '=', $kid->id)->delete(); //delete kid's photos $kid->photos()->delete(); } //delete kid record $kid->delete(); \Session::flash('flash_message', $kid->name . ' was deleted.'); return redirect('/kids'); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $kid = \p4\Kid::findOrFail($id); $kid->delete(); /** * Flash a session message to the screen */ \Session::flash('flash_message', 'You have cancelled registration for ' . $kid->fullname); return redirect('/cancelkidreg'); }
/** * Responds to requests to GET /photos for a list of photos belonging to a kid */ public function getIndex(Request $request) { // Get all the photos belonging to the selected user's kids // Sort in descending order by id $photos = \P4\Photo::with('kid')->select('photos.*')->leftJoin('kid_user', 'kid_user.kid_id', '=', 'photos.kid_id')->where('kid_user.user_id', \Auth::user()->id)->orderBy('photos.id', 'DESC')->get(); //Get a list of kids to let user know to add a kid record to start using the app $kids = []; if (sizeof($photos) == 0) { $kids = \P4\Kid::with('photos')->select('kids.*')->leftJoin('kid_user', 'kids.id', '=', 'kid_user.kid_id')->where('kid_user.user_id', \Auth::user()->id)->orderBy('kids.name', 'ASC')->get(); } return view('photos.index')->with('photos', $photos)->with('kids', $kids); }