/** * Run the database seeds. * * @return void */ public function run() { # First, create an array of all the apple types we want to associate users with # The *key* will be the apple type, and the *value* will be an array of users. $apples = ['fuji' => ['Jill'], 'golden_delicious' => ['Jill', 'Jamal'], 'red_delicious' => ['Jill', 'Jamal']]; foreach ($apples as $type => $users) { $apple = \App\Apple::where('type', 'LIKE', $type)->first(); if ($apple != null) { foreach ($users as $name) { $user = \App\User::where('name', 'LIKE', $name)->first(); if ($user != null) { $apple->users()->save($user); } } } } }