Add Cart model, resource and its API, Pivot table with Items.

Update Items model to have relationship with cart.
This commit is contained in:
2023-06-15 17:43:59 +03:00
parent ea363b956e
commit 4a4d5e7335
12 changed files with 388 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Cart>
*/
class CartFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}
@@ -0,0 +1,38 @@
<?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->unsignedBiginteger('city_id')->unsigned();
$table->unsignedBiginteger('company_id')->unsigned();
$table->unsignedBiginteger('user_id')->unsigned();
$table->string('status'); // CART | DONE
$table->foreign('city_id')->references('id')
->on('cities')->onDelete('cascade');
$table->foreign('company_id')->references('id')
->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')
->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('carts');
}
};
@@ -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('carts_items', function (Blueprint $table) {
$table->id();
$table->unsignedBiginteger('cart_id')->unsigned();
$table->unsignedBiginteger('item_id')->unsigned();
$table->foreign('cart_id')->references('id')
->on('carts')->onDelete('cascade');
$table->foreign('item_id')->references('id')
->on('items')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('carts_items');
}
};
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class CartSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}