是否可以访问和提取像地址这样的faker提供商的组件。例如,我只需要stateAbbr来填充数据库中的一列,而不是整个地址。
我尝试了类似$faker->address->stateAbbr
的内容但当然会抛出错误
// Faker\Provider\en_US\Address
cityPrefix // 'Lake'
secondaryAddress // 'Suite 961'
state // 'NewMexico'
stateAbbr // 'OH'
citySuffix // 'borough'
streetSuffix // 'Keys'
buildingNumber // '484'
city // 'West Judge'
streetName // 'Keegan Trail'
streetAddress // '439 Karley Loaf Suite 897'
postcode // '17916'
address // '8888 Cummings Vista Apt. 101, Susanbury, NY 95473'
country // 'Falkland Islands (Malvinas)'
latitude($min = -90, $max = 90) // 77.147489
longitude($min = -180, $max = 180) // 86.211205
//This is my code
use Faker\Generator as Faker;
$factory->define(App\Company::class, function (Faker $faker) {
return [
'city'=>$faker->city,
'state'=>$faker->address->stateAbbr,//Getiing an error here
];
});
答案 0 :(得分:2)
直接访问stateAbbr格式化程序
$factory->define(App\Company::class, function (Faker $faker) {
return [
'city'=>$faker->city,
'state'=>$faker->stateAbbr
];
});