-
Notifications
You must be signed in to change notification settings - Fork 28
[Proposal] Prefixed Eloquent Models #151
Comments
How is setting a prefix easier than just setting the table name directly? Even if you want to keep it in a config somewhere, you can still do this: class Post extends Model
{
public function __construct(array $attributes)
{
$this->table = config('modules.blog.database.prefix', 'blog_')).$this->getTable();
parent::__construct($attributes);
}
} |
Yeah i've done it long time ago and it's ugly. BTW, this is much cleaner: class Post extends Model
{
public function getTable()
{
return config('modules.blog.database.prefix', 'blog_')).parent::getTable();
}
} |
I like the idea of prefixing table names. This may serve as namespacing in database tables. Easier to navigate related tables. But this feature should be optional. |
Something like this can be accomplished using traits pretty simply. Also, using traits would make it an opt-in feature, like soft deletes. The following code is all based on the code posted by @arcanedev-maroc For example, the trait <?php
namespace App;
trait HasTablePrefix
{
/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return $this->getPrefix() . parent::getTable();
}
/**
* Get the prefix associated with the model.
*
* @return string
*/
public function getPrefix()
{
return is_null($this->prefix) ? '' : $this->prefix;
}
/**
* Set the prefix associated with the model.
*
* @param string $prefix
* @return $this
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
} Using it inside a <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasTablePrefix;
protected $prefix = 'blog_';
} However, if someone were to use this approach and had many models with a prefix that had to be updated this could prove to be a pain. We can do better by creating another trait (this trait would theoretically exist in user-land code, not in the core), say something like <?php
namespace App;
trait BlogPrefix
{
use HasTablePrefix;
/**
* The table prefix associated with the model.
*
* @var string
*/
protected $prefix = 'blog_';
} the final model might look something like this: <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use BlogPrefix;
} |
I have created a package for this idea, please help me review it |
This is useful when i create modules inside my app and separate the the db tables by prefixing each module tables.
For example:
shop_*
:shop_products
,shop_orders
,shop_shipping
…blog_*
:blog_posts
,blog_comments
,blog_categories
,blog_tags
…sys_*
:sys_users
,sys_roles
…So the solution that i found is to override the Illuminate Eloquent Model by doing this (Note: i didn't override the relationships methods like
joiningTable()
, but you can add the prefix inside the child class):And in a module like Blog (e.g.), i create a base Model Class (abstract) :
After that, the model itself (Post e.g.):
So, what is the benefit by using the prefixes ? The idea behind this is to make the code/db modular and/or extract it as a package, i do the same thing for the migration 😄.
And the best part is you can do something like this:
So i hope the
Illuminate\Database\Eloquent\Model
class implement the prefix feature like i did for more flexibility.Any feedbacks are welcome 👍
The text was updated successfully, but these errors were encountered: