Skip to content

Commit

Permalink
分类模型和数据
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 6, 2022
1 parent 6767857 commit 1da201b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
use HasFactory;

public $timestamps = false;

protected $fillable = [
'name', 'description',
];
}
23 changes: 23 additions & 0 deletions database/migrations/2022_03_06_111752_create_categories_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

return new class extends Migration
{
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name')->index()->comment('名称');
$table->text('description')->nullable()->comment('描述');
$table->integer('post_count')->default(0)->comment('帖子数');
});
}

public function down()
{
Schema::dropIfExists('categories');
}
};
38 changes: 38 additions & 0 deletions database/migrations/2022_03_06_112212_seed_categories_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

return new class extends Migration
{
public function up()
{
$categories = [
[
'name' => '分享',
'description' => '分享创造,分享发现',
],
[
'name' => '教程',
'description' => '开发技巧、推荐扩展包等',
],
[
'name' => '问答',
'description' => '请保持友善,互帮互助',
],
[
'name' => '公告',
'description' => '站点公告',
],
];

DB::table('categories')->insert($categories);
}

public function down()
{
DB::table('categories')->truncate();
}
};

0 comments on commit 1da201b

Please sign in to comment.