15 Mart 2022
Laravel Migration Suffix

fatihirday/suffix
paketi, suffixli tablolarınızın laravel migration ve modelleri ile uyumlu şekilde çalışması için tasarlanmıştır.
Kurulum
- Paketi kurmak için
composer
komutunu çalıştırın
composer require fatihirday/suffix
- Config ve migration dosyalarını publish et
php artisan vendor:publish --provider="Fatihirday\Suffixed\SuffixServiceProvier"
Response
* config/suffixed.php
* migrations/*_create_suffixes_table.php
- Paketteki Suffix listesini kullanmak için
php artisan migrate
Kendi listenizi kullanacaksanız migrations/*_create_suffixes_table.php
dosyasını silebilirsiniz.
Configuration
- Config dosyası
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 | Açıklama |
suffixes.table | suffix listesinin tutulduğu tablo |
suffixes.code | suffix kolonu |
suffixes.auto_create_suffixed_tables | suffixes.table tablosuna yeni kayıt eklendiğinde otomatik olarak suffix tabloları oluşturun |
suffix_auto_check | Suffix’li tablolar üzerinde çalışmak istediğinde tabloyu otomatik olarak kontrol eder |
merge_operator | tablo ismi ve suffix birleştirme operatörü |
suffix_max_length | Suffix maksimum uzunluğu. sınırsız olmak için null kullanın |
- Suffix Model
class Suffix extends Model
{
use HasFactory, SuffixList;
}
Kendi custom modelinizi kullanmak için, model’inizde use SuffixList;
trait’ini çağırın.
Örnekler
Suffix’li tablolar için Migration oluşturma
- Suffix listesine kayıt ekle
$row = new \Fatihirday\Suffixed\Models\Suffix();
$row->name = 'Fatih';
$row->code = 'fth';
$row->save();
- suffix’li tablolar için migration dosyası oluştur
php artisan make:migration-suffix CreateDemoTable
class CreateDenemeTable extends SuffixMigration implements SuffixSchame
{
protected $baseTableName = 'demo';
public function upSuffix(string $tableName)
{
Schema::create($tableName, function (Blueprint $table) {
$table->id();
$table->string('name', 30);
$table->timestamps();
});
}
public function downSuffix(string $tableName)
{
Schema::dropIfExists($tableName);
}
}
- migration çalıştır
php artisan migrate
DB den baktığınızda demo_fth
tablosunu görebilirsiniz.
- Model oluştur
use Fatihirday\Suffixed\Models\Suffixed;
class Demo extends Model
{
use HasFactory, Suffixed;
}
Suffixli tabloları kullanmak için modelinizde Suffixed
trait’ini kullanın
Suffix’li modelin kullanımı
Suffix’li tabloyu kontrol et
App\Models\Demo::checkSuffixCode('fth');
// Response : true || false
Model’e suffix tanımla
App\Models\Demo::setSuffixCode('fth')->toSql();
// Response : "select * from `demo_fth`"
Tam tablo adı
App\Models\Demo::setSuffixCode('fth')->getTable();
// Response : "demo_fth"
Suffix oku
App\Models\Demo::setSuffixCode('fth')->getSuffixCode();
// Response : "fth"
Örnek 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();