简体   繁体   中英

Why does Laravel sometimes use singular database table names when loading data?

I am working on a Laravel project that uses several models with plural database names. In this example, the model:

class RedCar extends Model

Is supported by the table:

red_cars

I have another model called Dog that has a belongsToMany relationship:

class Dog extends Model
{
    public function red_cars()
    {
        return $this->belongsToMany(RedCar::class)
        ->select( array('name','description') );
    }

There is a pivot table

dog_red_car

When I visit a page that loads several RedCar items (using the ->load() function), I get this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_database.red_car' doesn't exist (SQL: select name , version , description , dog_red_car . dog_id as pivot_dog_id , dog_red_car . red_car_id as pivot_red_car_id from dog_red_car inner join red_car on red_car . id = dog_red_car . red_car_id where dog_red_car . dog_id in (1024))

Notice how all of the red_car table names are singular. I am not sure what is causing Laravel to refer to the table as singular. The model currently has no protected $table variable, so Laravel should be defaulting the plural version. I have searched through the project and can't find any code that is (obviously) making reference to a database table named red_car . also, I can't find the word 'singular' used anywhere. The migrations shows a plural name:

Schema::create('red_cars', function (Blueprint $table) {

There are also several other places in code where the plural form of the table name is used. What could be making Laravel use the singular form of this table name? The project has several other belongsToMany relationships, but only one other appears to be having this problem. I managed to add a quick fix which corrected the error by adding a $table to the model:

protected $table = 'red_cars';

Apparently this forces Laravel to use red_cars (which is the correct plural form) and the page loads without an error. However, I am afraid there is a mysterious setting that will cause singular-plural database problems later.

Laravel, being a very simple framework that wants to be inclusive; tries to add a lot of functionalities in it to make the job of the developers who use it, a bit easier. One way it does it is through artisan commands that do a lot of the scaffolding in seconds and eliminating the need for painstaking filename coordination and management.

Laravel follows a strict naming convention that you can read up on, over here https://webdevetc.com/blog/laravel-naming-conventions

Have you run the

PHP artisan make:model RedCar -a

command?

The -a tag automatically creates 'all' the necessary files for that model; a migration, seeder, and controller.

You can also read up on other tags in Laravel's documentation. When the naming convention is followed, it eliminates the need for explicitly pointing to a table using

protected $table = 'red_cars';

Hope I was able to help you out.

Sometimes it just happens. It's best to add a default table name in your models so the Laravel will not make this mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM