laravel批量生成假数据

原创 2017年10月03日 00:02:06

D:\phpStudy\WWW\api.douxiaoli.com\database\factories\ModelFactory.php
D:\phpStudy\WWW\BCCKidV1.0\vendor\fzaninotto\faker\src\Faker\Generator.php

$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;
    #定义假数据长什么样子
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('test12345'),
        'api_token' => str_random(60),
        'remember_token' => str_random(10),
    ];
});

然后在命令行中运行:

php artisan thinker
factory(‘App\User’,10)->create();
这个地方的引号是英文的,csdn编辑器原因,复制出来之后是中文的,记得改成英文。

那个10表示一次生成10个假数据。

这里写图片描述

数据库结果记录:

这里写图片描述

其他可用的东东:

/**
 * @property string $name
 * @method string name(string $gender = null)
 * @property string $firstName
 * @method string firstName(string $gender = null)
 * @property string $firstNameMale
 * @property string $firstNameFemale
 * @property string $lastName
 * @property string $title
 * @method string title(string $gender = null)
 * @property string $titleMale
 * @property string $titleFemale
 *
 * @property string $citySuffix
 * @property string $streetSuffix
 * @property string $buildingNumber
 * @property string $city
 * @property string $streetName
 * @property string $streetAddress
 * @property string $postcode
 * @property string $address
 * @property string $country
 * @property float  $latitude
 * @property float  $longitude
 *
 * @property string $ean13
 * @property string $ean8
 * @property string $isbn13
 * @property string $isbn10
 *
 * @property string $phoneNumber
 *
 * @property string $company
 * @property string $companySuffix
 * @property string $jobTitle
 *
 * @property string $creditCardType
 * @property string $creditCardNumber
 * @method string creditCardNumber($type = null, $formatted = false, $separator = '-')
 * @property \DateTime $creditCardExpirationDate
 * @property string $creditCardExpirationDateString
 * @property array $creditCardDetails
 * @property string $bankAccountNumber
 * @method string iban($countryCode = null, $prefix = '', $length = null)
 * @property string $swiftBicNumber
 * @property string $vat
 *
 * @property string $word
 * @property string|array $words
 * @method string|array words($nb = 3, $asText = false)
 * @property string $sentence
 * @method string sentence($nbWords = 6, $variableNbWords = true)
 * @property string|array $sentences
 * @method string|array sentences($nb = 3, $asText = false)
 * @property string $paragraph
 * @method string paragraph($nbSentences = 3, $variableNbSentences = true)
 * @property string|array $paragraphs
 * @method string|array paragraphs($nb = 3, $asText = false)
 * @property string $text
 * @method string text($maxNbChars = 200)
 *
 * @method string realText($maxNbChars = 200, $indexSize = 2)
 *
 * @property string $email
 * @property string $safeEmail
 * @property string $freeEmail
 * @property string $companyEmail
 * @property string $freeEmailDomain
 * @property string $safeEmailDomain
 * @property string $userName
 * @property string $password
 * @method string password($minLength = 6, $maxLength = 20)
 * @property string $domainName
 * @property string $domainWord
 * @property string $tld
 * @property string $url
 * @property string $slug
 * @method string slug($nbWords = 6, $variableNbWords = true)
 * @property string $ipv4
 * @property string $ipv6
 * @property string $localIpv4
 * @property string $macAddress
 *
 * @property int       $unixTime
 * @property \DateTime $dateTime
 * @property \DateTime $dateTimeAD
 * @property string    $iso8601
 * @property \DateTime $dateTimeThisCentury
 * @property \DateTime $dateTimeThisDecade
 * @property \DateTime $dateTimeThisYear
 * @property \DateTime $dateTimeThisMonth
 * @property string    $amPm
 * @property int       $dayOfMonth
 * @property int       $dayOfWeek
 * @property int       $month
 * @property string    $monthName
 * @property int       $year
 * @property int       $century
 * @property string    $timezone
 * @method string amPm($max = 'now')
 * @method string date($format = 'Y-m-d', $max = 'now')
 * @method string dayOfMonth($max = 'now')
 * @method string dayOfWeek($max = 'now')
 * @method string iso8601($max = 'now')
 * @method string month($max = 'now')
 * @method string monthName($max = 'now')
 * @method string time($format = 'H:i:s', $max = 'now')
 * @method string unixTime($max = 'now')
 * @method string year($max = 'now')
 * @method \DateTime dateTime($max = 'now', $timezone = null)
 * @method \DateTime dateTimeAd($max = 'now', $timezone = null)
 * @method \DateTime dateTimeBetween($startDate = '-30 years', $endDate = 'now')
 * @method \DateTime dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null)
 * @method \DateTime dateTimeThisCentury($max = 'now', $timezone = null)
 * @method \DateTime dateTimeThisDecade($max = 'now', $timezone = null)
 * @method \DateTime dateTimeThisYear($max = 'now', $timezone = null)
 * @method \DateTime dateTimeThisMonth($max = 'now', $timezone = null)
 *
 * @property string $md5
 * @property string $sha1
 * @property string $sha256
 * @property string $locale
 * @property string $countryCode
 * @property string $countryISOAlpha3
 * @property string $languageCode
 * @property string $currencyCode
 * @property boolean $boolean
 * @method boolean boolean($chanceOfGettingTrue = 50)
 *
 * @property int    $randomDigit
 * @property int    $randomDigitNotNull
 * @property string $randomLetter
 * @property string $randomAscii
 * @method int randomNumber($nbDigits = null, $strict = false)
 * @method int|string|null randomKey(array $array = array())
 * @method int numberBetween($min = 0, $max = 2147483647)
 * @method float randomFloat($nbMaxDecimals = null, $min = 0, $max = null)
 * @method mixed randomElement(array $array = array('a', 'b', 'c'))
 * @method array randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
 * @method array|string shuffle($arg = '')
 * @method array shuffleArray(array $array = array())
 * @method string shuffleString($string = '', $encoding = 'UTF-8')
 * @method string numerify($string = '###')
 * @method string lexify($string = '????')
 * @method string bothify($string = '## ??')
 * @method string asciify($string = '****')
 * @method string regexify($regex = '')
 * @method string toLower($string = '')
 * @method string toUpper($string = '')
 * @method Generator optional($weight = 0.5, $default = null)
 * @method Generator unique($reset = false, $maxRetries = 10000)
 * @method Generator valid($validator = null, $maxRetries = 10000)
 *
 * @method integer biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt')
 *
 * @property string $macProcessor
 * @property string $linuxProcessor
 * @property string $userAgent
 * @property string $chrome
 * @property string $firefox
 * @property string $safari
 * @property string $opera
 * @property string $internetExplorer
 * @property string $windowsPlatformToken
 * @property string $macPlatformToken
 * @property string $linuxPlatformToken
 *
 * @property string $uuid
 *
 * @property string $mimeType
 * @property string $fileExtension
 * @method string file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true)
 *
 * @method string imageUrl($width = 640, $height = 480, $category = null, $randomize = true, $word = null, $gray = false)
 * @method string image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null)
 *
 * @property string $hexColor
 * @property string $safeHexColor
 * @property string $rgbColor
 * @property array $rgbColorAsArray
 * @property string $rgbCssColor
 * @property string $safeColorName
 * @property string $colorName
 *
 * @method string randomHtml($maxDepth = 4, $maxWidth = 4)
 *
 */
版权声明:经验之谈,不知能否换包辣条,另转载请注明出处。 https://blog.csdn.net/zhezhebie/article/details/78153265

如何生成比较像样的假数据

 问题在做项目的时候经常会遇到这样的问题: 根据数据模型建立了数据库,但是数据库中却没有数据,在给客户做Demo的时候必须要一条一条的添加假数据,而且这些假数据还得像模像样的,不能乱输入,尽是看...
  • studyzy
  • studyzy
  • 2010-06-30 19:56:00
  • 12478

Laravel数据库操作的三种方式

Laravel提供了3种操作数据库方式:DB facade(原始方式)、查询构造器和Eloquent ORM。下面我将逐一讲解。 数据库的配置文件在config目录下的database.php里。打...
  • zls986992484
  • zls986992484
  • 2016-10-15 20:45:35
  • 52978

mock.js 生成假数据 前端独立于后台开发

mock.js的作用是产生假数据,使得前端的开发独立于后台开发,而后台发开者$(document).ready(function() { var template = { 'results|1-...
  • rbb317
  • rbb317
  • 2015-06-12 18:17:29
  • 3813

laravel批量生成假数据

D:\phpStudy\WWW\api.douxiaoli.com\database\factories\ModelFactory.php D:\phpStudy\WWW\BCCKidV1...
  • zhezhebie
  • zhezhebie
  • 2017-10-03 00:02:06
  • 713

假数据

String str = "[{\"text\": \"提升额度\",\"current\": \"-1\"},{\"text\": \"提交申请\"}, {\"text\": \"审核中\"},{\...
  • aicpzl
  • aicpzl
  • 2016-11-09 09:17:06
  • 204

Laravel 5使用faker Model Factory生成网站模拟演示数据

开发中,假如我需要一些演示的数据,那么,我可能得连接数据库,创建一大堆的演示数据,这是一件非常蛋疼的事情。还好,Laravel是优雅高效的,Laravel绝对不会干很蠢的事情。 按照大多数人的做...
  • chajinglong
  • chajinglong
  • 2017-05-05 12:46:38
  • 1471

使用 Laravel 数据填充功能生成中文测试数据

今晚……不对,是昨晚,折腾一个的小项目,发现自动填充的中文数据显示起来总不太美观,于是开始琢磨如何填充中文数据进行测试。 然而一番搜索后惊奇的发现,官方、以及一些非官方的文档均未提及这一功能。期...
  • luyaran
  • luyaran
  • 2017-07-07 16:06:14
  • 669

JSON数据造假和解析

客户端开发通常要从服务器获取数据,所以就需要调用后台的接口,然后获得后台返回的数据,根据这些数据的格式进行JSON解析。 由于后台返回的数据会有不同的格式,所以前端也要采取不同的JSON.parse(...
  • shan286
  • shan286
  • 2015-12-24 20:41:09
  • 2514

【Keras】使用数据生成器(data generators)解决训练数据内存问题

原文地址: A detailed example of how to use data generators with Keras 引言 在使用kears训练model的时候,一般会将所有...
  • m0_37477175
  • m0_37477175
  • 2018-03-27 18:16:46
  • 61

laravel批量添加

前台表单页面 Document ">
  • zhangzhangdan
  • zhangzhangdan
  • 2016-10-10 19:43:25
  • 3287
收藏助手
不良信息举报
您举报文章:laravel批量生成假数据
举报原因:
原因补充:

(最多只允许输入30个字)