PHP code example of laragear / meta-model

1. Go to this page and download the library: Download laragear/meta-model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laragear / meta-model example snippets


namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Laragear\MetaModel\CustomMigration;
use Laragear\MetaModel\HasCustomization;

class MyPackageModel extends Model
{
    use HasCustomization;
    
    protected static function migration(): string
    {
        return CustomMigration::make(function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        })
    }
}

namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Laragear\MetaModel\HasCustomization;

class Car extends Model
{
    use HasCustomization;
    
    // ...
}

use Vendor\Package\Models\Car;

use Illuminate\Foundation\Application;

return Application::booted(function () {

    Car::customize(function ($model) {
        $model->setTable('vendor_cars');
        $model->setConnection('read-database');
    });
    
})->create();

namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Laragear\MetaModel\CustomMigration;
use Laragear\MetaModel\HasCustomization;
use MyVendor\MyPackage\Models\Car;

class Car extends Model
{
    use HasCustomization;
    
    public static function makeMigrations(): CustomMigration|array
    {
        return CustomMigration::make(function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });
    }
}

// database/migrations/0000_00_00_000000_create_cars_table.php
use Vendor\Package\Models\Car;

return Car::migration();

namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Laragear\MetaModel\CustomMigration;
use Laragear\MetaModel\HasCustomization;
use MyVendor\MyPackage\Models\Car;

class Car extends Model
{
    use HasCustomization;
    
    public static function makeMigrations(): CustomMigration|array
    {
        return [
            CustomMigration::make(function (Blueprint $table) {
                // ...
            }),
            CustomMigration::make(function (Blueprint $table) {
                // ...
            }, table: 'car_repairs'),
        ];
    }
}

// database/migrations/0000_00_00_000000_create_cars_table.php
use MyVendor\MyPackage\Models\Car;

return Car::migration();

// database/migrations/0000_00_00_000000_create_car_repairs_table.php
use MyVendor\MyPackage\Models\Car;

return Car::migration('car_repairs');

use Laragear\MetaModel\CustomMigration;

public static function migration(): CustomMigration
{
    return CustomMigration::make(function (Blueprint $table) {
        $table->id();
        
        $this->createMorph($table, 'ownable');
        
        $table->string('manufacturer');
        $table->string('model');
        $table->tinyInteger('year');
        
        $table->timestamps();
    });
}

// database/migrations/0000_00_00_000000_create_cars_table.php
use Vendor\Package\Models\Car;

return Car::migration()->morph('ulid', 'custom_index_name');

use Laragear\MetaModel\CustomMigration;

public static function migration(): CustomMigration
{
    return CustomMigration::make(function (Blueprint $table) {
        // ...
        
        $this->createMorphRelation($table, 'ownable', 'ownable_table_index');
    }
}

// database/migrations/0000_00_00_000000_create_cars_table.php
use Vendor\Package\Models\Car;

// Uses "custom_index_name" as index name
return Car::migration()->morph('ulid', 'custom_index_name');

// Uses "ownable_table_index" as index name
return Car::migration()->morph('ulid');

use MyVendor\MyPackage\Models\Car;
use Illuminate\Database\Schema\Blueprint;

return Car::migration()
    ->afterUp(function (Blueprint $table) {
        $table->foreign('manufacturer')->references('name')->on('manufacturers');
    })
    ->beforeDown(function (Blueprint $table) {
         $table->dropForeign('manufacturer');
    });