简体   繁体   中英

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

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?

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

$faker->latitude(-90, 90)

Or:

$faker->latitude()

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM