function load_lastnames($limit = 500) { if (!DB::schema('mock-data')->hasTable('last_names')) { DB::schema('mock-data')->create('last_names', function ($table) { # Define some fields. $table->increments('id'); $table->string('name', 15); $table->integer('rank'); # Add some indexes. $table->index('rank'); }); } else { LastName::truncate(); } $fp = fopen(get_filename('lastnames.txt'), 'r'); $count = 0; while (!feof($fp)) { if ($count > $limit - 1) { return $count; } else { $line = trim(fgets($fp)); if (strlen($line) > 0) { $row = unpack("A14name/A7freq/A7cumulfreq/A6rank", trim($line)); $lastname = new LastName(); $lastname->name = ucfirst(strtolower(trim($row['name']))); $lastname->rank = ucfirst(strtolower(trim($row['rank']))); $lastname->save(); $count++; } else { continue; } } } fclose($fp); return $count; }
/** * Generate a Last Name * * Uses US Census data to get $max most popular names for both male and female and selects one at random * * Pool is only 250 most frequent last names. Increase by passing a higher value for $max * * @param int $max How large should our pool of names be? Default: 250 * @return string Last Name */ public function getLastName($max = 250) { return LastName::where('rank', '<=', $max)->orderByRaw(Database::random())->first()->name; }