fatihirday / suffix
laravel migration suffixed tables
v0.1.4
2024-08-19 12:25 UTC
Requires
- php: ^7.4|^8.0
README
Installation
- Run composer command to install the package
composer require fatihirday/suffix
- Publish the config and migration files.
php artisan vendor:publish --provider="Fatihirday\Suffixed\SuffixServiceProvier"
- Response
* config/suffixed.php
* migrations/*_create_suffixes_table.php
- To use the suffix list in the package
php artisan migrate
If you are going to use your own list, you can delete the file migrations/*_create_suffixes_table.php
Configuration
- Config suffixed File
return [ 'suffixes' => [ 'table' => \Fatihirday\Suffixed\Models\Suffix::class, 'column' => 'code', 'auto_create_suffixed_tables' => true, ], 'suffix_auto_check' => true, 'merge_operator' => '_', 'suffix_max_length' => 3, ];
Config | Des |
---|---|
suffixes.table |
suffix list table |
suffixes.code |
suffix column |
suffixes.auto_create_suffixed_tables |
Automatically create suffixes tables when new record is added to suffixes.table table |
suffix_auto_check |
When it wants to work on the attached table, it automatically checks the table. |
merge_operator |
table name and suffix merge operator |
suffix_max_length |
suffix max length. use `null to be unlimited |
- Suffix Model
class Suffix extends Model { use HasFactory, SuffixList; }
use SuffixList;
for custom model.
Examples
Make migration
1. Insert row to suffixes table
php artisan tinker
$row = new \Fatihirday\Suffixed\Models\Suffix(); $row->name = 'Fatih'; $row->code = 'fth'; $row->save();
2. Make migration suffixed
php artisan make:migration-suffix CreateDemoTable
class CreateDenemeTable extends SuffixMigration implements SuffixSchame { protected $baseTableName = 'demo'; public function upSuffix(string $tableName, string $prefix) { Schema::create($tableName, function (Blueprint $table) use ($prefix) { $table->id(); $table->string('name', 30); $table->timestamps(); }); } public function downSuffix(string $tableName, string $prefix) { Schema::dropIfExists($tableName); } }
3. Run the migrate
php artisan migrate
Created table demo_fth
4. Make Model
Use Suffixed in your model to access suffixed tables
class Demo extends Model { use HasFactory, Suffixed; }
Use of suffix table
Check suffixed table
App\Models\Demo::checkSuffixCode('fth'); // Response : true || false
Set suffix code
App\Models\Demo::setSuffixCode('fth')->toSql(); // Response : "select * from `demo_fth`"
Get suffixed table name
App\Models\Demo::setSuffixCode('fth')->getTable(); // Response : "demo_fth"
Get suffixed table suffix code
App\Models\Demo::setSuffixCode('fth')->getSuffixCode(); // Response : "fth"
example query
// insert row to demo_fth table $newRow = App\Models\Demo::setSuffixCode('fth'); $newRow->name = 'deneme'; $newRow->save(); // get rows to demo_fth table App\Models\Demo::setSuffixCode('fth')->whereNotNull('name')->get();