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


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(): CustomMigration
    {
        return CustomMigration::create(function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });
    }
}

namespace Vendor\Package\Models;

use Illuminate\Database\Eloquent\Model;
use Laragear\MetaModel\HasCustomization;
use Vendor\Package\Migrations\CarMigration;

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

use Vendor\Package\Models\PackageModel;

use Illuminate\Foundation\Application;

return Application::booted(function () {

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

namespace MyVendor\MyPackage\Models;

use Closure;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 migration(): CustomMigration
    {
        return CustomMigration::create(function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });
    }
}

// database/migrations/0000_00_00_000000_create_cars_table.php

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

return Car::migration();

use Laragear\MetaModel\CustomMigration;

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

use MyVendor\MyPackage\Models\Car;

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

use Laragear\MetaModel\CustomMigration;

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

use MyVendor\MyPackage\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');
    });