11

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?

Community
  • 1
  • 1
Jaylen
  • 39,043
  • 40
  • 128
  • 221

2 Answers2

12

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

$faker->latitude(-90, 90)

Or:

$faker->latitude()
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I get this error `[Symfony\Component\Debug\Exception\FatalErrorException] Call to undefined method Faker\Factory::latitude()` it seems that I need to pull a different faker library. this is what I have in my composer.json file under the require-dev section `"fzaninotto/faker": "~1.4",` – Jaylen Oct 24 '16 at 18:30
  • @Jaylen, I've just tested it and it works. I've got same version as you are. Try to run `composer update`, maybe package wasn't installed correctly. – Alexey Mezenin Oct 24 '16 at 18:35
  • I just finished updating everything. but still get this error ` [Symfony\Component\Debug\Exception\FatalErrorException] Call to undefined method Faker\Factory::latitude()` – Jaylen Oct 24 '16 at 19:10
0

The problem is the you have mixed up Faker's object and static reference. You should either use it with static reference like

Faker::Address.latitude 

or if you wanna use it with object do like this

$faker = new Faker(); $faker->address()->latitude;
Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36