如何使用 Laravel 播种机中的 Faker 生成纬度、经度信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40224904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:39:17  来源:igfitidea点击:

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

phplaravelfaker

提问by Jaylen

I am trying to use Fakerin a Laravel seeder.

我正在尝试在 Laravel 播种机中使用Faker

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

[ErrorException] 未定义的属性:Faker\Factory::$Address

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

我也尝试像这样访问经度属性,$faker->longitude但仍然无效。

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

如何访问 faker 中的经度属性以生成数据?

回答by Alexey Mezenin

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

您应该添加latitute和其他人作为属性。所以,尝试这样的事情:

$faker->latitude(-90, 90)

Or:

或者:

$faker->latitude()

回答by Nitesh Verma

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

问题是你混淆了 Faker 的对象和static引用。您应该将它与静态引用一起使用,例如

Faker::Address.latitude 

or if you wanna use it with object do like this

或者如果你想将它与对象一起使用,请这样做

$faker = new Faker(); $faker->address()->latitude;