Laravel Migration Suffix

Laravel Suffix

fatihirday/suffix paketi, suffixli tablolarınızın laravel migration ve modelleri ile uyumlu şekilde çalışması için tasarlanmıştır.

Kurulum

  1. Paketi kurmak için composer komutunu çalıştırın
composer require fatihirday/suffix
  1. Config ve migration dosyalarını publish et
php artisan vendor:publish --provider="Fatihirday\Suffixed\SuffixServiceProvier"

Response

* config/suffixed.php
* migrations/*_create_suffixes_table.php
  1. Paketteki Suffix listesini kullanmak için
php artisan migrate

Kendi listenizi kullanacaksanız migrations/*_create_suffixes_table.php dosyasını silebilirsiniz.

Configuration

  1. 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,
];
ConfigAçıklama
suffixes.tablesuffix listesinin tutulduğu tablo
suffixes.codesuffix kolonu
suffixes.auto_create_suffixed_tablessuffixes.table tablosuna yeni kayıt eklendiğinde otomatik olarak suffix tabloları oluşturun
suffix_auto_checkSuffix’li tablolar üzerinde çalışmak istediğinde tabloyu otomatik olarak kontrol eder
merge_operatortablo ismi ve suffix birleştirme operatörü
suffix_max_lengthSuffix maksimum uzunluğu. sınırsız olmak için null kullanın
  1. 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

  1. Suffix listesine kayıt ekle
$row = new \Fatihirday\Suffixed\Models\Suffix();
$row->name = 'Fatih';
$row->code = 'fth';
$row->save();
  1. 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);
    }
}
  1. migration çalıştır
php artisan migrate

DB den baktığınızda demo_fth tablosunu görebilirsiniz.

  1. 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();

Yorum Ekle

E-posta hesabınız yayımlanmayacak.

Gerçek kişi doğrulaması
53 − 50 =