From e540fd3b6d39410ff27f614c51ed0c95236788da Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 19 Mar 2012 14:11:11 -0500 Subject: [PATCH] Automatically detect eloquent table names. --- laravel/database/eloquent/model.php | 11 +++++++++++ .../relationships/has_many_and_belongs_to.php | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index c8522beb..fc7b0380 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -1,5 +1,6 @@ exists or $this->original !== $this->attributes; } + /** + * Get the name of the table associated with the model. + * + * @return string + */ + public function table() + { + return static::$table ?: strtolower(Str::plural(basename(get_class($this)))); + } + /** * Get the dirty attributes for the model. * diff --git a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php index 7f3c01b0..23b8765a 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -1,5 +1,6 @@ model->get_timestamp(); $attributes['updated_at'] = $attributes['created_at'];