美文网首页
php faker 伪造数据

php faker 伪造数据

作者: 常圆 | 来源:发表于2017-01-05 20:57 被阅读420次

    Faker 是一个用来生成数据库实例数据的工具。Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

    installation

    composer require fzaninotto/faker

    basic usage

    Use Faker\Factory::create() to create and initialize a faker generator, which can generate data by accessing properties named after the type of data you want.

    <?php
    
    require_once '/path/to/Faker/src/autoload.php';
    $faker = Faker\Factory::create();
    echo $faker->name;
    echo $faker->address;
    echo $faker->text;
    echo $faker->email;
    
    

    src/Faker/Generator.php 中狠多种类型可以参考,包括file,image,uuid 各种数据类型。

    在laravel 中的使用

    Laravel artisan 的 tinker 是一个 REPL (read-eval-print-loop) ,REPL 是指 交互式命令行界面,它可以让你输入一段代码去执行,并把执行结果直接打印到命令行界面里。通过php artisan tinker 和faker结合之后,可以直接用来测试。Tinker 是 Laravel 自带的 REPL,基于 PsySH 构建而来。同样需要psysh的支持。

    使用示例如下:

    php artisan tinker
    //这是是使用faker的工厂模式生成数据,其中App\User需要在database\factories\ModelFactories 文件中提前写好需要设置的数据类型和字段
    factory("App\User::class",10)->create(); 
    //查询
    APP\User::all(); 
    App\User::count();
    //增加数据
    $user = new App\User;
    $user->name = "Wruce Bayne";
    $user->email = "iambatman@savegotham.com";
    $user->save();
    // 删除数据
    $user = App\User::find(1);
    $user->delete();
    
    // 还可以查看doc帮助
    doc dd
    doc app
      
    // 显示源代码
     show <functionName>
    

    相关文章

      网友评论

          本文标题:php faker 伪造数据

          本文链接:https://www.haomeiwen.com/subject/wtzzvttx.html