How does eloquent recognize tables?

前端 未结 4 1259
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 02:07

I am curious about how eloquent knows in which table it should save the records we give it by running $ php artisan tinker . I do not actually remember setting

4条回答
  •  鱼传尺愫
    2021-01-13 02:22

    Actually if you not set the $table property, Eloquent will automatically look the snake case and plural name of the class name. For example if class name is User, it will users.

    Here the code taken from Illuminate/Database/Eloquent/Model.php

    public function getTable()
    {
        if (isset($this->table)) {
            return $this->table;
        }
    
        return str_replace('\\', '', Str::snake(Str::plural(class_basename($this))));
    }
    

提交回复
热议问题