标签归档:laravel

lumen 使用 redis

安装扩展

composer require predis/predis
composer require illuminate/redis

引入redis支持

$app->register(Illuminate\Redis\RedisServiceProvider::class);

启用redis辅助函数

$app->withFacades();
$app->withEloquent();

配置redis服务器参数

默认系统是调用的.env里的redis配置文件,但是一般安装后没有这些参数,可以查看文件路径vendor/laravel/lumen-framework/config/database.php中查看有哪些参数需要配置,例如,我的.env文件需要配置

REDIS_HOST=192.168.1.41
REDIS_PORT=7000
REDIS_PASSWORD=123456

laravel 5.5 安装使用 passport

使用 Composer 安装 Passport :

composer require laravel/passport:~4.0

Laravel 5.6 已经发布,最新的 laravel/passport 版本为 5.0 适配 Laravel 5.6,所以我们需要使用 Laravel 5.5 的适配版本 4.0

如果安装过程中遇到 paragonie/random_compat 版本冲突的问题,可以先执行以下命令将其降级为 2.0 的版本。

composer require paragonie/random_compat:^2.0

继续阅读

faker 常见填充的类型

[
    // 个人信息  
    '姓名 name' => $faker->name,
    '姓 lastName' => $faker->lastName,
    '名 firstName' => $faker->firstName,
    '手机 mobile' => $faker->phoneNumber,
    '信用卡信息数组 creditCardDetails'=> $faker->creditCardDetails,

    // 地理位置  
    '纬度 latitude' =>$faker->latitude,
    '经度 longitude' =>$faker->longitude,
    '国家 country' => $faker->country,
    '国家代码 countryCode' => $faker->countryCode,
    '邮政编码:postcode'=> $faker->postcode,
    '地址 address' => $faker->address,
    '城市 city' => $faker->city,
    '省 state'=> $faker->state,
    '省简称 stateAbbr' =>$faker->stateAbbr,
    '区 area' => $faker->area,
    '街道 streetAddress' => $faker->streetAddress,
    '邮编 postcode' => $faker->postcode,

    // 公司,银行,广告语  
    '公司后缀 companySuffix'=> $faker->companySuffix,
    '公司 company'=> $faker->company,
    '银行 bank' => $faker->bank,
    '广告语 catchPhrase'=>$faker->catchPhrase,

    // 时间  
    'unixTime' => $faker->unixTime,
    'year' => $faker->year,
    'month' => $faker->month,
    'dayOfMonth'=> $faker->dayOfMonth,
    'date' =>$faker->date,
    'time' =>$faker->time,

    //互联网  
    'domainName' => $faker->domainName,
    'companyEmail' => $faker->companyEmail,
    'email' => $faker->email,
    'userName' => $faker->userName,
    'password' => $faker->password,
    'url' => $faker->url,
    'ipv4' => $faker->ipv4,
    'ipv6' => $faker->ipv6,
    'localIpv4' => $faker->localIpv4,
    'macAddress' => $faker->macAddress,
    'userAgent' => $faker->userAgent,

    // 杂项  
    '罗马数字century' => $faker->century,
    '颜色hexcolor' =>$faker->hexcolor,
    'mimeType'=>$faker->mimeType,
    '文件后缀fileExtension'=>$faker->fileExtension,
    'img' => $faker->image,
    'imgurl' => $faker->imageUrl,
    'uuid'   => $faker->uuid,
    'sha1' => $faker->sha1,
    'sha256' => $faker->sha256,
    'md5'=> $faker->md5,

    //文本内容  
    '单词 word' => $faker->word,
    '单词 words' => $faker->words ( 5, true ),
    '短标题 sentence' => $faker->sentence,
    '内容 content' => $faker->text ( 500 ) ,
    '内容 realText' => $faker->realText(100),
]

laravel使用php-redis和prefix防止redis键名冲突

首先需要安装 php-redis

apt install php-redis

config/database.php 中,修改 redisclientphpredis,并在 default 中添加 prefix => env('REDIS_PREFIX', ''),

最后在 .env 中配置 REDIS_PREFIX 即可实现隔离。

但是,直接使用 redis 要比使用 php-redis 效率更高。

laravel 在 seeder 中使用 faker

use Illuminate\Database\Seeder;

use App\Comment;

class CommentsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Let's truncate our existing records to start from scratch.
        Comment::truncate();

        $faker = \Faker\Factory::create();

        // And now, let's create a few articles in our database:
        for ($i = 0; $i < 100; $i++) {
            Comment::create([
                'post_id' => random_int(1, 50),
                'user_id' => random_int(1, 14),
                'status' => random_int(0, 1),
                'content' => $faker->sentence,
            ]);
        }
    }
}