/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker::create();
     $animal_id = \CareCats\Animal::where('sub_species', '=', 'tiger')->pluck('id');
     DB::table('procedures')->insert(['created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'date' => $faker->date($format = 'Y-m-d', $max = 'now'), 'animal_id' => $animal_id, 'title' => 'Tooth Extraction', 'symptoms' => "Unwillingness to eat, poor temperment", 'comments' => 'took 1 hour, successful!']);
     $animal_id = \CareCats\Animal::where('sub_species', '=', 'lion')->pluck('id');
     DB::table('procedures')->insert(['created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'date' => $faker->date($format = 'Y-m-d', $max = 'now'), 'animal_id' => $animal_id, 'title' => 'Eye removal', 'symptoms' => "Failing eyesite, poor temperment", 'comments' => 'took 3 hours, successful!']);
     $animal_id = \CareCats\Animal::where('sub_species', '=', 'bobcat')->pluck('id');
     DB::table('procedures')->insert(['created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'date' => $faker->date($format = 'Y-m-d', $max = 'now'), 'animal_id' => $animal_id, 'title' => 'Wound suture', 'symptoms' => "Open wound, seeping, bloody", 'comments' => 'took 2 hours, successful!']);
     $animal_id = \CareCats\Animal::where('sub_species', '=', 'cougar')->pluck('id');
     DB::table('procedures')->insert(['created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'date' => $faker->date($format = 'Y-m-d', $max = 'now'), 'animal_id' => $animal_id, 'title' => 'Lethargy', 'symptoms' => "General malaise, little movement", 'comments' => 'Brought inside, kept under observation for two days']);
     $animal_id = \CareCats\Animal::where('sub_species', '=', 'leopard')->pluck('id');
     DB::table('procedures')->insert(['created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'date' => $faker->date($format = 'Y-m-d', $max = 'now'), 'animal_id' => $animal_id, 'title' => 'Arthritis', 'symptoms' => "stiff and achy", 'comments' => 'Brought inside on the recent cold days, given movies to watch and treats and blankets']);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     # First, create an array of all the books we want to associate tags with
     # The *key* will be the book title, and the *value* will be an array of tags.
     //    $books =[
     //        'The Great Gatsby' => ['novel','fiction','classic','wealth'],
     //        'The Bell Jar' => ['novel','fiction','classic','women'],
     //        'I Know Why the Caged Bird Sings' => ['autobiography','nonfiction','classic','women']
     //    ];
     # First, create an array of all the animals we want to associate sponsors with
     # The *key* will be the animal name, and the *value* will be an array of sponsor first_names.
     $animal_model1 = DB::table('animals')->where('id', '=', 1)->first();
     $animal_model2 = DB::table('animals')->where('id', '=', 2)->first();
     $animal_model3 = DB::table('animals')->where('id', '=', 3)->first();
     $animal_model4 = DB::table('animals')->where('id', '=', 4)->first();
     $animal_model5 = DB::table('animals')->where('id', '=', 5)->first();
     $animal1 = $animal_model1->name;
     $animal2 = $animal_model2->name;
     $animal3 = $animal_model3->name;
     $animal4 = $animal_model4->name;
     $animal5 = $animal_model5->name;
     $sponsor_model1 = DB::table('sponsors')->where('id', '=', 1)->first();
     $sponsor_model2 = DB::table('sponsors')->where('id', '=', 2)->first();
     $sponsor_model3 = DB::table('sponsors')->where('id', '=', 3)->first();
     $sponsor_model4 = DB::table('sponsors')->where('id', '=', 4)->first();
     $sponsor_model5 = DB::table('sponsors')->where('id', '=', 5)->first();
     $sponsor1 = $sponsor_model1->first_name;
     $sponsor2 = $sponsor_model2->first_name;
     $sponsor3 = $sponsor_model3->first_name;
     $sponsor4 = $sponsor_model4->first_name;
     $sponsor5 = $sponsor_model5->first_name;
     $animals = [$animal1 => [$sponsor1, $sponsor2, $sponsor3, $sponsor4], $animal2 => [$sponsor2, $sponsor3, $sponsor4, $sponsor5], $animal3 => [$sponsor3, $sponsor4, $sponsor5, $sponsor1], $animal4 => [$sponsor4, $sponsor5, $sponsor1, $sponsor2], $animal5 => [$sponsor5, $sponsor1, $sponsor2, $sponsor3]];
     # Now loop through the above array, creating a new pivot for each book to tag
     // foreach($books as $title => $tags) {
     foreach ($animals as $name => $sponsors) {
         # First get the book
         // $book = \App\Book::where('title','like',$title)->first();
         $animal = \CareCats\Animal::where('name', 'like', $name)->first();
         # Now loop through each tag for this book, adding the pivot
         // foreach($tags as $tagName) {
         foreach ($sponsors as $sponsorName) {
             //$tag = \App\Tag::where('name','LIKE',$tagName)->first();
             $sponsor = \CareCats\Sponsor::where('first_name', 'LIKE', $sponsorName)->first();
             # Connect this tag to this book
             //$book->tags()->save($tag);
             $animal->sponsors()->save($sponsor);
         }
     }
 }
示例#3
0
Route::get('/eagerCat', function () {
    # If you're querying for many things, you may want to join in the related data with the query. This can be done via the with() method and is referred to as eager loading:
    # Eager load the animal with the procedure
    $procedures = \CareCats\Procedure::with('animal')->get();
    foreach ($procedures as $procedure) {
        echo 'Name: ' . $procedure->animal->name . ', procedure: ' . $procedure->title . '<br>';
    }
});
Route::get('/sponsorCat', function () {
    // $book = \App\Book::where('title','=','The Great Gatsby')->first();
    //
    // echo $book->title.' is tagged with: ';
    // foreach($book->tags as $tag) {
    //     echo $tag->name.' ';
    // }
    $animal = \CareCats\Animal::where('name', '=', 'Matt')->first();
    echo '<br /><br />' . $animal->name . ' is sponsored by: ';
    foreach ($animal->sponsors as $sponsor) {
        echo $sponsor->first_name . ', ';
    }
});
Route::get('/sponsorCat2', function () {
    #eagerly loaded
    $animals = \CareCats\Animal::with('sponsors')->get();
    foreach ($animals as $animal) {
        echo '<br>' . $animal->name . ' is sponsored by: ';
        foreach ($animal->sponsors as $sponsor) {
            echo $sponsor->first_name . ', ';
        }
    }
});