我正在尝试让模型工厂工作,主要模型地点如下:
- 是 hasOne 关系的地方
- 具有 belongsTo 关系的类型
最重要的是,place 有一个名为 full_address 的函数,它返回一个逗号分隔的字符串。
场地有一个 toSearchableArray() 将自定义数据发送到 Algolia,当我运行工厂创建方法时,我得到:
PHP Error: Call to a member function full_address() on null in app/Venue.php on line 57
这是场地模型
class Venue extends Model
{
use Searchable;
public function toSearchableArray()
{
$type = $this->type()->first();
$place = $this->place()->first();
return [ 'name' => $this->name,
'type' => $type['name'],
'type_id' => $type['id'],
'venue_id' => $this->id,
'background_image' =>$type['background_file_name'],
'full_address' => $place->full_address(),
'slug' => $this->slug_field,
'_geoloc' =>
[ 'lat' => (float)$this->latitude,
'lng' => (float)$this->longitude
]
];
}
public function place()
{
return $this->hasOne('App\Place');
}
public function type()
{
return $this->belongsTo('App\Type');
}
}
这些是模型工厂
$factory->define(App\Venue::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'phoneNumber' => $faker->word,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude,
'LatLong' => $faker->word,
'website' => $faker->word,
'type' => $faker->word,
'place_id' => function () {
return factory(App\Place::class)->create()->id;
},
'status' => $faker->word,
'slug_field' => $faker->word,
'type_id' => function () {
return factory(App\Type::class)->create()->id;
},
];
});
$factory->define(App\Place::class, function (Faker\Generator $faker) {
return [
'place_id' => $faker->randomNumber(),
'administrative_area_level_2' => $faker->word,
'administrative_area_level_1' => $faker->word,
'country' => $faker->country,
'postal_town' => $faker->word,
'postal_code_prefix' => $faker->word,
'postal_code' => $faker->word,
'route' => $faker->word,
'locality' => $faker->word,
'premise' => $faker->word,
'street_number' => $faker->word,
'neighborhood' => $faker->word,
'point_of_interest' => $faker->word,
'natural_feature' => $faker->word,
'administrative_area_level_3' => $faker->word,
'postal_code_suffix' => $faker->word,
'sublocality_level_1' => $faker->word,
'subpremise' => $faker->word,
'sublocality_level_5' => $faker->word,
'sublocality_level_4' => $faker->word,
'sublocality_level_2' => $faker->word,
'sublocality_level_3' => $faker->word,
'administrative_area_level_4' => $faker->word,
'colloquial_area' => $faker->word,
'long' => '123',
'lat' => '123',
];
});
$factory->define(App\Type::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'marker_file_name' => $faker->word,
'background_file_name' => $faker->word,
'description' => $faker->word,
'ShortName' => $faker->word,
];
});
我需要做什么才能获得与场地相关的地点的参考。
谢谢