My understanding is that the naming convention for database tables in Laravel is to use plural names for regular tables, and singular names for pivot tables (docs). I have a users table, a locations table, and a location_user table.
When I'm attempting to seed the location_user table, I get an error message that Laravel can't find the location_users (plural) table.
When I first ran into this, I thought I must have the naming convention wrong, so I changed it to 1ocation_users, which allowed it to seed without any issues, but then I got an error from eloquent when trying to access the relationship that seemed to indicate it was looking for the singular version of the table.
NOTE: I'm aware I can name the table whatever I want and pass that into the relationship, but I don't want to do that. I want to have the table names be what the Laravel convention is.
Here's the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'location_users' doesn't exist (SQL: truncate table `location_users`)
I don't have any model relationships defined between the models. I've also verified that my migration was run and the table does exist in the DB:
Here's my table definition:
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('location_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignIdFor(Location::class)->constrained();
$table->foreignIdFor(User::class)->constrained();
$table->boolean('is_primary')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('location_user');
}
};
and here's my seeder:
class LocationUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
LocationUser::truncate();
$locationUser = new LocationUser([
'location_id' => 1,
'user_id' => 1,
'is_primary' => true,
]);
$locationUser->save();
}
}
