[Seri] - Website đồng hồ - Tạo Model - Migrate table cho bảng danh mục

Tạo csdl và bảng danh mục ( categories) và model bằng lệnh trong laravel

Tiếp tục seri hôm nay mình sẽ tạo molde, migrate cho bảng danh mục sản phẩm

Model

Các bạn tạo bằng lệnh sau 

php artisan make:model Models/Category

Các bạn chú ý là model mình sẽ lưu vào trong thư mục app/Models nhé. Còn mặc định laravel ở trong thư mục app 

Nội dung file Category.php như sau 

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';
    
}

Migrate

Các bạn tạo bằng lệnh 

php artisan make:migration create_categories_table

Nội dung file sẽ là 

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

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('c_name');
            $table->string('c_slug')->unique();
            $table->string('c_avatar')->nullable();
            $table->string('c_banner')->nullable();
            $table->string('c_description')->nullable();
            $table->tinyInteger('c_hot')->default(0);
            $table->tinyInteger('c_status')->default(1);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

Link video

Để lại comment của bạn nếu gặp khó khăn

Bài viết liên quan