Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Don't use a global prefix, but use a custom table name per model instead :


class Group extends Eloquent {

    protected $table = 'myprefix_group';


}

0

I haven't fully implemented this yet but this is what I've got going.

I have a config value for the db.prefix

then for migrations

	public function __construct()
	{
		// Get the prefix
		$this->prefix = Config::get('db.prefix', '');
	}

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
//		Schema::table('users', function(Blueprint $table)
		Schema::table($this->prefix . 'users', function(Blueprint $table)
		{
			//
		});
	}

In the model

	protected $table = Config::get('db.prefix', '') . 'users';

If you use raw queries in your model/repositories don't forget to include the config value there too.

IF you add it to the config/database.php file then all your tables will have the prefix. So, I assign them independently. It's more work but you get finer control.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Nymos nymos Joined 9 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.