How laravel finds a relationship between models and its table in a database

in my model i

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { // } 

in my controller, I can say Page:all() and get all the rows from the pages table but I donโ€™t see the connection between the Page model and the pages table in the database

just guessing the name of the table based on the model name (lower case with extra s at the end) or is it mentioned somewhere else?

+5
source share
1 answer

As you can see in the docs, this is Laravel's magic :-)

https://laravel.com/docs/5.2/eloquent#defining-models (see Table Names)

If you want, you can manually set a different username

protected $table = 'my_table_name';

And to go a little further, so Laravel gets the name of the table in the base model, which you can find in /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)))); } 
+4
source

All Articles