我想让一个模型工厂工作,主要模型Venue如下:
除此之外,place还有一个名为full_address的函数,它返回一个逗号分隔的字符串。
Venue有一个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,
];
});
我需要做些什么来获得与场地相关的地方的参考。
由于