laravel数据库迁移-数据填充

php artisan make:seeder FriendsHashMobileSeeder
  • 编写代码
<?php

use Illuminate\Database\Seeder;

class FriendsHashMobileSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $friends_invitations = \App\Models\Api\FriendsInvitationsModel::query()
            ->where('status','pending')
            ->get();
        if (!empty($friends_invitations)){
            $friends_invitations = $friends_invitations->toArray();
            foreach ($friends_invitations as $k => $v){
                \Illuminate\Support\Facades\DB::table('friends')->insert([
                    'sender_id'         => $v['sender_id'],
                    'hash_mobile'       => $v['hash_mobile'],
                    'status'            => 'pending',
                    'created_at'        => $v['created_at']
                ]);
            }
        }
    }
}
  • 执行迁移命令
php artisan db:seed --class FriendsHashMobileSeeder

 

© 版权声明
THE END
喜欢就支持一下吧
分享