hretic hretic - 1 year ago 39
PHP Question

how does laravel finds the connection between models and its table in database

in my model i have


namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model

in my controller i can say
and get all the rows from
but i dont see any connection between
model and
table in the database

does it just guess table name based on the model name (lower case with extra s at the end ) or it's mentioned somewhere else ?

Answer Source

As you can see in the docs, this is the magic of Laravel :-) (see Table Names)

If you want, you can set another name manually by user the following

protected $table = 'my_table_name';

And to go a bit further, this is how Laravel gets the table name in the base Model you can found at /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php

 * Get the table associated with the model.
 * @return string
public function getTable()
    if (isset($this->table)) {
        return $this->table;

    return str_replace('\\', '', Str::snake(Str::plural(class_basename($this))));