使用Laravel种子和sql文件填充数据库 [英] Use Laravel seed and sql files to populate database

查看:55
本文介绍了使用Laravel种子和sql文件填充数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从github上获得了一些国家,州和城市的.sql文件.如何使用Laravel的种子文件运行它们以填充数据库中的那些表?

I got several .sql files of countries, states and cities of the world from github. How can I run them with Laravel's seed files to populate those tables in my database?

推荐答案

  1. DB :: unprepared()添加到 DatabaseSeeder 的运行方法中.
  2. 在命令行中运行 php artisan db:seed .

  1. Add DB::unprepared() to the run method of DatabaseSeeder.
  2. Run php artisan db:seed at the command line.

class DatabaseSeeder extends Seeder {

    public function run()
    {
        Eloquent::unguard();

        $this->call('UserTableSeeder');
        $this->command->info('User table seeded!');

        $path = 'app/developer_docs/countries.sql';
        DB::unprepared(file_get_contents($path));
        $this->command->info('Country table seeded!');
    }
}

这篇关于使用Laravel种子和sql文件填充数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆