1. 程式人生 > >laravel模型建立和數據遷移和數據填充(數據填充沒有成功)未完

laravel模型建立和數據遷移和數據填充(數據填充沒有成功)未完

-m paragraph AD 技術分享 user blue get databases art

開始創建我們的第一個 Article 模型及其對應遷移文件了,我們在項目根目錄運行如下 Artisan 命令一步到位:

php artisan make:model Article -m

-m--migration 的縮寫,告知 Artisan 在創建模型同時創建與之對應的遷移文件(我使用的是 Laradock 作為開發環境):

技術分享圖片

當然,還需要編輯默認生成的遷移文件:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create(‘articles‘, function (Blueprint $table) { $table->increments(‘id‘); $table->string(‘title‘);
$table->text(‘body‘); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists(‘articles‘); } }

然後我們運行如下命令創建對應數據表:

php artisan migrate

技術分享圖片

現在我們回到 Article

模型類添加如下屬性到 $fillable 字段以便可以在 Article::createArticle::update方法中可以使用它們:

class Article extends Model
{
    protected $fillable = [‘title‘, ‘body‘];
}

數據庫填充

Laravel 通過 Faker 庫可以快速為我們生成格式正確的測試數據:

php artisan make:seeder ArticlesTableSeeder

生成的填充器類位於 /database/seeds 目錄下,我們編輯填充器類如下:

use Illuminate\Database\Seeder;
use App\Article;

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

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

        // And now, let‘s create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            Article::create([
                title => $faker->sentence,
                body => $faker->paragraph,
            ]);
        }
    }
}

然後運行填充命令:

php artisan db:seed --class=ArticlesTableSeeder

重復上述過程創建一個用戶填充器:

use Illuminate\Database\Seeder;
use App\User;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Let‘s clear the users table first
        User::truncate();

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

        // Let‘s make sure everyone has the same password and
        // let‘s hash it before the loop, or else our seeder
        // will be too slow.
        $password = Hash::make(‘toptal‘);

        User::create([
            ‘name‘ => ‘Administrator‘,
            ‘email‘ => ‘[email protected]‘,
            ‘password‘ => $password,
        ]);

        // And now let‘s generate a few dozen users for our app:
        for ($i = 0; $i < 10; $i++) {
            User::create([
                ‘name‘ => $faker->name,
                ‘email‘ => $faker->email,
                ‘password‘ => $password,
            ]);
        }
    }
}

編輯 DatabaseSeeder 類:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(ArticlesTableSeeder::class);
    }
}

後運行 php artisan db:seed 就可以執行所有填充器填充數據。

數據填充怎麽沒有成功?

參考 (轉):http://laravelacademy.org/post/9153.html

laravel模型建立和數據遷移和數據填充(數據填充沒有成功)未完