0

I am trying to get a model factory to work, the main model Venue the following:

  1. Place which is a hasOne relation
  2. Type which has a belongsTo relation

On top of that, place has a function called full_address, that returns a comma separated string.

The Venue has a toSearchableArray() to send custom data to Algolia, when I run the factory create method I get:

PHP Error:  Call to a member function full_address() on null in  app/Venue.php on line 57

This is the venue Model

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');
    }

}

And these are the model factories

$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,
    ];
});

what do I need to do in order to get a reference to the place that is associated with the venue.

Thanks

JaChNo
  • 1,493
  • 9
  • 28
  • 56
  • Where do you run the `factory` and use the function `toSearchableArray()` ? – Felippe Duarte May 16 '18 at 16:12
  • the factory is run using tinker, >>> factory('App\Venue')->create(). in toSearchableArray is part of laravel scout – JaChNo May 16 '18 at 16:18
  • Can you dd($this->place_id, $this->place) in toSearchableArray()? – Julien Bourdeau May 22 '18 at 13:06
  • Basically this was a chicken and egg problem. it was not creating the relationship between place and venue becasue the insert of venue was failing due to place being null. this was null becasue there was not a venue_id being created to add to place. – JaChNo May 25 '18 at 09:46

0 Answers0