3vuedo 数据库和seed:laravel项目学习
3vuedo
Post 表
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->unique();
$table->text('description');
$table->text('content');
$table->integer('created_by')->unsigned();
$table->timestamps();
$table->foreign('created_by')->references('id')->on('users');
});
}
Post表加参数
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->smallInteger('status')->default(3);
$table->dateTime('moderated_at')->nullable();
$table->integer('moderated_by')->nullable()->unsigned();
$table->foreign('moderated_by')->references('id')->on('users');
});
}
User表加参数
Schema::table('users', function (Blueprint $table) {
$table->integer('role_level')->unsigned()->default(1);
$table->foreign('role_level')->references('level')->on('roles');
});
category_post表
public function up()
{
Schema::create('category_post', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');;
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');;
$table->timestamps();
});
}
Seed还是得看代码
图片表,
public function run()
{
DB::table('media')->truncate();
$faker = Faker\Factory::create();
foreach (\App\Post::all() as $post) {
if(rand(1, 10) > 4){
$counter = 0;
// max retries = 5 because sometimes faker return false
while (!($fakeImage = $faker->image(null, 600, 400)) && ($counter < 5)) {
$counter++;
}
if ($fakeImage !== false) {
$post->addMedia($fakeImage)->preservingOriginal()->toCollectionOnDisk('featured', 'local-media');
}
}
}
}
}

浙公网安备 33010602011771号