From f3108bf6af3fda8d446511a162cc87f875230ec4 Mon Sep 17 00:00:00 2001 From: Ainun Nadhifah Syamsiyah Date: Tue, 8 Oct 2024 21:50:14 +0700 Subject: [PATCH] feat: init db --- app/Models/Cart.php | 28 +++++++++++++++ app/Models/CartProduct.php | 23 +++++++++++++ app/Models/Product.php | 18 ++++++++++ app/Models/Transaction.php | 18 ++++++++++ app/Models/User.php | 5 +++ ..._083802_add_role_column_to_users_table.php | 28 +++++++++++++++ .../2024_10_08_141250_create_cart_table.php | 28 +++++++++++++++ ..._10_08_142821_create_transaction_table.php | 29 ++++++++++++++++ ...10_08_143730_create_cart_product_table.php | 30 ++++++++++++++++ ...2024_10_08_144442_create_product_table.php | 34 +++++++++++++++++++ 10 files changed, 241 insertions(+) create mode 100644 app/Models/Cart.php create mode 100644 app/Models/CartProduct.php create mode 100644 app/Models/Product.php create mode 100644 app/Models/Transaction.php create mode 100644 database/migrations/2024_10_08_083802_add_role_column_to_users_table.php create mode 100644 database/migrations/2024_10_08_141250_create_cart_table.php create mode 100644 database/migrations/2024_10_08_142821_create_transaction_table.php create mode 100644 database/migrations/2024_10_08_143730_create_cart_product_table.php create mode 100644 database/migrations/2024_10_08_144442_create_product_table.php diff --git a/app/Models/Cart.php b/app/Models/Cart.php new file mode 100644 index 0000000..4c5fed3 --- /dev/null +++ b/app/Models/Cart.php @@ -0,0 +1,28 @@ +belongsTo(User::class); + } + + public function transactions() + { + return $this->hasOne(Transaction::class); + } + + public function cartProducts() + { + return $this->hasMany(CartProduct::class); + } +} diff --git a/app/Models/CartProduct.php b/app/Models/CartProduct.php new file mode 100644 index 0000000..0efedeb --- /dev/null +++ b/app/Models/CartProduct.php @@ -0,0 +1,23 @@ +belongsTo(Cart::class); + } + + public function product() + { + return $this->belongsTo(Product::class); + } +} diff --git a/app/Models/Product.php b/app/Models/Product.php new file mode 100644 index 0000000..dc5e31d --- /dev/null +++ b/app/Models/Product.php @@ -0,0 +1,18 @@ +hasMany(CartProduct::class); + } +} diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php new file mode 100644 index 0000000..7c7ce97 --- /dev/null +++ b/app/Models/Transaction.php @@ -0,0 +1,18 @@ +belongsTo(Cart::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index def621f..e702329 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -44,4 +44,9 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function carts() + { + return $this->hasOne(Cart::class); + } } diff --git a/database/migrations/2024_10_08_083802_add_role_column_to_users_table.php b/database/migrations/2024_10_08_083802_add_role_column_to_users_table.php new file mode 100644 index 0000000..1a0f8b4 --- /dev/null +++ b/database/migrations/2024_10_08_083802_add_role_column_to_users_table.php @@ -0,0 +1,28 @@ +string('role')->default('user'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + // + }); + } +}; diff --git a/database/migrations/2024_10_08_141250_create_cart_table.php b/database/migrations/2024_10_08_141250_create_cart_table.php new file mode 100644 index 0000000..9931c78 --- /dev/null +++ b/database/migrations/2024_10_08_141250_create_cart_table.php @@ -0,0 +1,28 @@ +id(); + $table->foreignId('user_id')->constrained('users')->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('carts'); + } +}; diff --git a/database/migrations/2024_10_08_142821_create_transaction_table.php b/database/migrations/2024_10_08_142821_create_transaction_table.php new file mode 100644 index 0000000..80677d3 --- /dev/null +++ b/database/migrations/2024_10_08_142821_create_transaction_table.php @@ -0,0 +1,29 @@ +id(); + $table->foreignId('cart_id')->constrained('carts')->onDelete('cascade'); + $table->decimal('total_price', 10, 2); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('transactions'); + } +}; diff --git a/database/migrations/2024_10_08_143730_create_cart_product_table.php b/database/migrations/2024_10_08_143730_create_cart_product_table.php new file mode 100644 index 0000000..5cc6b04 --- /dev/null +++ b/database/migrations/2024_10_08_143730_create_cart_product_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('cart_id')->constrained('carts')->onDelete('cascade'); + $table->foreignId('product_id')->constrained('products')->onDelete('cascade'); + $table->integer('quantity')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('cart_products'); + } +}; diff --git a/database/migrations/2024_10_08_144442_create_product_table.php b/database/migrations/2024_10_08_144442_create_product_table.php new file mode 100644 index 0000000..a9ff9e7 --- /dev/null +++ b/database/migrations/2024_10_08_144442_create_product_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('name'); + $table->text('description'); + $table->decimal('price', 10, 2); + $table->integer('stock'); + $table->string('image_url'); + $table->decimal('rating', 2, 1); + $table->string('city'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('products'); + } +};