Laravel 9 Cannot find table - pivot table singular/plural issue

Viewed 288

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:

enter image description here

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();

    }
}
2 Answers

Try to set your table name at your model

<?php

namespace App\Models;

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

class LocationUser extends Model
{
    use HasFactory;

    protected $table = 'location_user';
}

try this to defining foreign keys in your migrations:

$table->unsignedBigIntieger('location_id');
$table->foreign('location_id')->references('id')->on('locations');

$table->unsignedBigIntieger('user_id');
$table->foreign('user_id')->references('id')->on('users');

OR

$table->foreignId('user_id')->constrained('users');

you must use table names in migrations, migration knows nothing about models.

Related