PHP code example of dandoetech / laravel-model-meta

1. Go to this page and download the library: Download dandoetech/laravel-model-meta 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/ */

    

dandoetech / laravel-model-meta example snippets


class ProductResource extends Resource implements HasEloquentModel
{
    public function model(): string { return \App\Models\Product::class; }

    protected function define(ResourceBuilder $b): void
    {
        $b->key('product')
          ->label('Product')
          ->timestamps()
          ->softDeletes()
          ->field('name', FieldType::String, nullable: false, rules: ['



declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

final class Product extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $table = 'products';

    /** @var list<string> */
    protected $fillable = [
        'name',
        'price',
        'category_id',
    ];

    /** @var array<string, string> */
    protected $casts = [
        'price' => 'float',
        'category_id' => 'integer',
    ];

    public function category()
    {
        return $this->belongsTo(\App\Models\Category::class, 'category_id');
    }
}

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->float('price');
            $table->unsignedBigInteger('category_id');
            $table->timestamps();
            $table->softDeletes();
            $table->foreign('category_id')->references('id')->on('categories');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};

return [
    // Map resource keys to Eloquent models (for introspection)
    'resource_to_model' => [
        // 'product' => App\Models\Product::class,
    ],

    // Extra fields not discoverable by introspection
    'array_fields' => [
        // 'product' => [new FieldDefinition('virtual_flag', FieldType::Boolean, false)],
    ],

    // Provider priority (first non-empty result wins)
    'provider_order' => [
        ArrayModelMetaProvider::class,
        IntrospectingModelMetaProvider::class,
    ],
];
bash
php artisan vendor:publish --tag=model-meta-config
bash
php artisan model-meta:generate-models
php artisan model-meta:generate-migrations