The Silent Failure in migrate:fresh --seed
You ran php artisan migrate:fresh --seed, and you expected fresh data in your database. Instead, Laravel threw an exception saying it couldn't find the table named product. The stack trace is clear: Table 'e-commerce.product' doesn't exist. At first glance, this looks like a migration failure or a missing seed file, but the real culprit is a simple mismatch between how you defined your model and how you named your database schema.
The error message explicitly points to product (singular), yet your migration file creates a table called products (plural). This discrepancy stems from the Product model configuration:
class Product extends Model
{
protected $table = 'product'; // <-- The mismatch
protected $fillable = ['name', 'image', 'type', 'price', 'description'];
}
When you define protected $table, you are overriding Laravel's default convention. By default, Laravel guesses the table name by converting the model class name to snake_case (e.g., Product becomes products). However, your migration explicitly names the table products:
Schema::create('products', function (Blueprint $table) {
// ...
});
Here lies the conflict. The migration runs successfully and creates a table named products. However, when you execute ProductTableSeeder, Laravel attempts to run an insert query against the product table because of the $table property in your model. Since product does not exist, the database throws the "Base table or view not found" error.
Resolving the Mismatch
To fix this, you have two straightforward options. The first is to align the model with the migration file. Update the Product model to remove the explicit table definition, allowing Laravel to use its default convention which matches your migration:
class Product extends Model
{
// Removed 'protected $table = "product";'
protected $fillable = ['name', 'image', 'type', 'price', 'description'];
}
The second option is to update the model's $table property to match the migration exactly:
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['name', 'image', 'type', 'price', 'description'];
}
Either approach resolves the immediate error, but consistency is key. Always ensure that the table name referenced in the model matches the physical table created by the migration.
Data Type Considerations
While addressing the table name issue, there is another potential pitfall hidden in your migration file regarding data types. You defined the price column as follows:
$table->decimal('price', 2);
In Laravel migrations, the scale parameter (the second argument) defines the number of decimal places allowed. A scale of 2 means you can store values like 1000.98, but attempting to insert a value larger than 99.99 will trigger a database error. In your seeder, you are inserting prices like 3000 and 2000.50. Depending on the specific MySQL configuration and driver version, inserting values exceeding the defined precision without sufficient total digits can cause issues or silent truncation.
It is generally safer to define the scale with enough room for your expected data range. For example, if prices can go up to thousands, a definition like $table->decimal('price', 10, 2) would be more robust than limiting it to just two total digits. This ensures that your seeder data inserts correctly without risking overflow errors in production environments.
Final Steps
Once you have corrected the model's table name and adjusted the decimal precision if necessary, run php artisan migrate:fresh --seed again. The migration will recreate the products table, and the seeder will successfully populate it with your sample data. Always double-check model configurations against migration definitions when seeding fails, as these mismatches are a frequent source of confusion for developers new to Laravel.