用于手机号码的控制器未知格式的 Laravel faker

Posted

技术标签:

【中文标题】用于手机号码的控制器未知格式的 Laravel faker【英文标题】:Laravel faker inside controller unknown format for mobileNumber 【发布时间】:2022-01-14 04:28:54 【问题描述】:

我在我的 Laravel 项目中使用 php Faker,我需要在播种器文件而不是工厂文件中使用 Faker,我正在从工厂文件中迁移它,我收到了一个错误:

未知格式“mobileNumber”

这在工厂里可以正常工作,但为什么我的种子文件不行?

我错过了什么?

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Application;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Hashids\Hashids;
use Faker\Factory as Faker;

class ApplicationsSeeder extends Seeder

    private $applicants = [];

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    
        $faker = Faker::create();

        for ($i = 0; $i < 51; $i++) 
            $hashids = new Hashids('', 7);
            $bank = $faker->bankAccountNumber('', '', 8);
            $sort = rand(111111, 999999);

            $hashable = $bank . '' . $sort;
            $hashable = trim($hashable);

            // email formatting
            $email = $faker->safeEmail ?? null;
            $email = trim(str_replace(' ', '', $email));

            // postcode formatting
            $postcode = $faker->postcode ?? null;
            $postcode = trim(strtoupper(str_replace(' ', '', $postcode)));

            // mobile formatting
            $mobile = $faker->mobileNumber ?? null;
            $mobile = trim(str_replace(' ', '', $mobile));

            $application = [
              'ApiKey' => Str::random(35),
              'AffId' => "aff2020",
              'Application' => [
                'AppFirstName' => $faker->firstName,
                'AppLastName' => $faker->lastName,
                'AppEmail' => $email,
                'BankSortcode' => $sort,
                'BankAccount' => $bank,
                'AppMobilePhone' => $mobile
              ]
            ];

            $applicants[] = [
                'hash' => $hashids->encode($hashable),
                'product_type' => 'payday_form_honeycomb_faker',
                'email' => $email,
                'birthday' => Carbon::now()->subYears(rand(20, 50))->subMonths(1, 12)->subDays(1, 25)->toDateString(),
                'postcode' => $postcode,
                'mobile' => $mobile,
                'data' => json_encode($application),
                'verified_at' => Carbon::now(),
                'created_at' => Carbon::now()->toDateTimeString(),
                'updated_at' => Carbon::now()->toDateTimeString()
            ];
        

        foreach ($applicants as $applicant) 
            Applicant::insert($applicant);
        
    

【问题讨论】:

【参考方案1】:

您可以改用phoneNumber,例如$faker-&gt;phoneNumber;

【讨论】:

以上是关于用于手机号码的控制器未知格式的 Laravel faker的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 迁移未知数据库 [1049]

Laravel 5.8:SQLSTATE [42S22]:找不到列:1054 未知列

SQLSTATE [42S22]:未找到列:1054 laravel 4 中“字段列表”中的未知列“id”

未知类型的文件,怎样判别文件格式

POST http://localhost:8000/offers-ajax/store 419 (未知状态) Laravel AJAX

CodeBasePHP检查未知媒体文件的格式