Search code examples
sqllaravellaravel-artisanlaravel-seeding

Laravel ,php -artisan suffixe had to my table when i seed it


I have 2 small issues with the "php artisan db:seed" command.

  1. WHen i run the command, i have this error message :

"SQLSTATE[42S02] Base table or view not found : 1146 La table "bootstrap_template_commerciauxes" n'existe pas ..."

The problem is : my table name is commerciaux, and not commerciauxes. I checked all my file, my model is Commerciaux.php, my factory CommerciauxFactory. So ... what kind of sorcely is it ? I'am missing something ?

  1. Secondly, the SQL request from db:seed add some columns i dont want to :

    SQLSTATE[42S02]: Base table or view not found: 1146 La table 'bootstrap_template.commerciauxes' n'existe pas (SQL: insert into commerciauxes (nom, prenom, ville, updated_at, created_at) values (Dr. Luis Champlin PhD, Dr. Luella Leuschke, Leathaberg, 2022-06-03 21:42:44, 2022-06-03 21:42:44))

Here is my Commerciaux model :

<?php

namespace App\Models;

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

class Commerciaux extends Model
{
    use HasFactory;

    protected $fillable = [
        'nom',
        'prenom',
        'ville',
        'nbre_commande',
        
    ];
}

My CommerciauxFactory (in case)

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class CommerciauxFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'nom' => $this->faker->name(),
            'prenom' => $this->faker->name(),
            'ville' => $this->faker->city(),
        ];
    }
}

Thanks you very much for your time, i wanted to try this nice tool but i get blocked since 2 days on thoses mistakes.


Solution

  • To answer your issues:

    1. Laravel by default treats table names as plural due to default conversion and I would advise keeping it that way. If you want to define your own table name then, In your model Class you can define following for your name of table:

      protected $table = 'commerciaux';

    Also, in your migration's Up function, set your table name like following:

    public function up()
        {
            Schema::create('commerciaux', function (Blueprint $table) {
               //Your table columns and structure
            });
        }
    
    1. Regarding the additional columns, those are laravel timestamps that keep a track of the timestamps of the record when it was created (created_at) and updated(updated_at) last time. In this case, I would also suggest keeping these fields as they keep a track of record creation and last modifying timestamps. If you don't want these fields in your table then in your model you can define the following code to exclude the timestamps:

      public $timestamps = false;

    Other than that, you can also remove following line from your migration:

    table->timestamps();
    

    EDIT: Before running migration again, try the roll back command so the created base table and migration records can get deleted from the migrations table.