Prefix tables in Laravel to group them logically

Advertisements

I am using Laravel to build a buying and selling app. In consequnce, there are a couple of tables that are related to buying and selling. To group them all together instead of relying on alphabetic order (in PhpMyAdmin, Adminer, TablePlus, …), I want to prefix the tables with selling_ and buying_.

In my eyes this makes totally sense, because I could find tables quicker. I would like to this as well for spatie’s permissions table (model_has_permissions, model_has_roles, role_has_permissions, roles, permissions), because they are "flying around", as well as the jobs (jobs, failed_jobs) and users (users, personal_access_tokens, settings).

The new Pulse package is already doing that, what I really like.

So my question is: Is this a good idea and how would I realize that?
Because the downside I see: Using php artisan make:migration I would not concentrate on the prefix. I want to handle the prefix on the model.

>Solution :

Adding prefixes to your table names for better organization is a reasonable approach, and it can indeed make it easier to locate related tables, especially as your application grows. Laravel allows you to customize the table names in your Eloquent models.

Here’s how you can implement this in Laravel:

1. Update the Eloquent Model:

In each Eloquent model, specify the custom table name using the $table property:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BuyingItem extends Model
{
    protected $table = 'buying_items';

    // rest of the model...
}
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SellingItem extends Model
{
    protected $table = 'selling_items';

    // rest of the model...
}

2. Database Migrations:

When creating migrations using php artisan make:migration, you can manually set the table name in the generated migration file:

// Example migration file for buying_items table
public function up()
{
    Schema::create('buying_items', function (Blueprint $table) {
        $table->id();
        // other columns...
        $table->timestamps();
    });
}

3. Spatie Permissions Table:

For the Spatie permissions tables, you can use the spatie configuration file to define custom table names. Publish the configuration file:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

Then, in config/permission.php, you can customize the table names:

// config/permission.php
return [

    // ...

    'table_names' => [
        'roles' => 'your_prefix_roles',
        'permissions' => 'your_prefix_permissions',
        'model_has_permissions' => 'your_prefix_model_has_permissions',
        'model_has_roles' => 'your_prefix_model_has_roles',
        'role_has_permissions' => 'your_prefix_role_has_permissions',
    ],

    // ...

];

4. Jobs and Users:

Similar to other models, you can set the table names in your Eloquent models and migration files for jobs and users.

5. Considerations:

  • Make sure to update foreign keys in your migrations to reflect the
    new table names.
  • Document the table naming conventions for your
    team’s reference.

Overall, using prefixes for better organization is a good idea, and Laravel’s flexibility allows you to implement it according to your preferences.

Leave a ReplyCancel reply