How to generate latitude, longitude info using Faker from Laravel seeder?

Jaylen picture Jaylen · Oct 24, 2016 · Viewed 8.2k times · Source

I am trying to use Faker in a Laravel seeder.

Here is how my seeder class look like

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class LatitudeLongitudeTestTableSeeder extends Seeder
{

    public function run()
    {
        $faker = new Faker;

        $myTable = 'LatitudeLongitudeTest';

        foreach (range(1,10) as $index) {
            DB::table($myTable)->insert([
                'Latitude' => $faker->Address->latitude,
                'Longitude' => $faker->Address->longitude,
                'name' => $faker->Address->street_name,
            ]);
        }

    }
}

but this is giving me the following error

[ErrorException] Undefined property: Faker\Factory::$Address

I also tried to access the longitude property like this $faker->longitude but that still did not work.

How can I access the longitude property in faker to generate data?

Answer

Alexey Mezenin picture Alexey Mezenin · Oct 24, 2016

You should add latitute and others as properties. So, try something like this:

$faker->latitude(-90, 90)

Or:

$faker->latitude()