Commit e540fd3b authored by Taylor Otwell's avatar Taylor Otwell

Automatically detect eloquent table names.

parent 26afb000
<?php namespace Laravel\Database\Eloquent; <?php namespace Laravel\Database\Eloquent;
use Laravel\Str;
use Laravel\Database; use Laravel\Database;
use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To; use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
...@@ -418,6 +419,16 @@ abstract class Model { ...@@ -418,6 +419,16 @@ abstract class Model {
return ! $this->exists or $this->original !== $this->attributes; return ! $this->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. * Get the dirty attributes for the model.
* *
......
<?php namespace Laravel\Database\Eloquent\Relationships; <?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Str;
use Laravel\Database\Eloquent\Model; use Laravel\Database\Eloquent\Model;
use Laravel\Database\Eloquent\Pivot; use Laravel\Database\Eloquent\Pivot;
...@@ -48,7 +49,7 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -48,7 +49,7 @@ class Has_Many_And_Belongs_To extends Relationship {
/** /**
* Determine the joining table name for the relationship. * Determine the joining table name for the relationship.
* *
* By default, the name is the models sorted and concatenated with an underscore. * By default, the name is the models sorted and joined with underscores.
* *
* @return string * @return string
*/ */
...@@ -140,7 +141,7 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -140,7 +141,7 @@ class Has_Many_And_Belongs_To extends Relationship {
{ {
// All joining tables get creation and update timestamps automatically even though // All joining tables get creation and update timestamps automatically even though
// some developers may not need them. This just provides them if necessary since // some developers may not need them. This just provides them if necessary since
// it would be a pain for the developer to maintain them manually. // it would be a pain for the developer to maintain them each manually.
$attributes['created_at'] = $this->model->get_timestamp(); $attributes['created_at'] = $this->model->get_timestamp();
$attributes['updated_at'] = $attributes['created_at']; $attributes['updated_at'] = $attributes['created_at'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment