Skip to content

Commit

Permalink
feat: init db
Browse files Browse the repository at this point in the history
  • Loading branch information
ainunns committed Oct 8, 2024
1 parent d4e0285 commit f3108bf
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/Models/Cart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

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

class Cart extends Model
{
use HasFactory;

protected $fillable = ['user_id'];

public function user()
{
return $this->belongsTo(User::class);
}

public function transactions()
{
return $this->hasOne(Transaction::class);
}

public function cartProducts()
{
return $this->hasMany(CartProduct::class);
}
}
23 changes: 23 additions & 0 deletions app/Models/CartProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

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

class CartProduct extends Model
{
use HasFactory;

protected $fillable = ['cart_id', 'product_id', 'quantity'];

public function cart()
{
return $this->belongsTo(Cart::class);
}

public function product()
{
return $this->belongsTo(Product::class);
}
}
18 changes: 18 additions & 0 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

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

class Product extends Model
{
use HasFactory;

protected $fillable = ['name', 'description', 'price', 'stock', 'image_url', 'rating', 'city'];

public function cartProducts()
{
return $this->hasMany(CartProduct::class);
}
}
18 changes: 18 additions & 0 deletions app/Models/Transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

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

class Transaction extends Model
{
use HasFactory;

protected $fillable = ['cart_id', 'total_price'];

public function cart()
{
return $this->belongsTo(Cart::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ protected function casts(): array
'password' => 'hashed',
];
}

public function carts()
{
return $this->hasOne(Cart::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('role')->default('user');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
28 changes: 28 additions & 0 deletions database/migrations/2024_10_08_141250_create_cart_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('carts');
}
};
29 changes: 29 additions & 0 deletions database/migrations/2024_10_08_142821_create_transaction_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('transactions', function (Blueprint $table) {
$table->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');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cart_products', function (Blueprint $table) {
$table->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');
}
};
34 changes: 34 additions & 0 deletions database/migrations/2024_10_08_144442_create_product_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->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');
}
};

0 comments on commit f3108bf

Please sign in to comment.