Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 1
Create the product migration
First, create a Product Model and a Migration and add the code below in the migration:
<?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('product_name');
$table->string('product_desc');
$table->string('product_price');
$table->string('product_image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
Create the product factory
Next, let's create a product factory and add the code below inside:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
*/
class ProductFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
'product_name' => fake()->word(),
'product_desc' => fake()->paragraph(),
'product_price' => fake()->numberBetween(1, 1000),
'product_image' => fake()->imageUrl(640, 480),
];
}
}
Create the product seeder
Next, let's create a product seeder and add the code below inside:
<?php
namespace Database\Seeders;
use App\Models\Product;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
Product::factory(20)->create();
}
}
Seed the database
Next, let's seed the database and create 20 products update the file 'DatabaseSeeder.php' add the code below inside, and run the command:
php artisan db:seed
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
ProductSeeder::class
]);
}
}