PHP code example of watheqalshowaiter / model-fields

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

    

watheqalshowaiter / model-fields example snippets


Schema::create('users', function (Blueprint $table) {
    $table->id(); // primary key
    $table->string('name'); // e
    $table->string('password'); // 

// Facade way
use WatheqAlshowaiter\ModelFields\Fields;
use App\Models\User;

Fields::model(User::class)->allFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'password', 'random_number', 'remember_token', 'created_at', 'updated_at']
Fields::model(User::class)->

// Macro way
User::allFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'password', 'random_number', 'remember_token', 'created_at', 'updated_at']
User::

Schema::create('posts', function (Blueprint $table) {
    $table->uuid('id')->primary(); // primary key
    $table->foreignId('user_id')->constrained(); // llable(); // nullable (but will be changed later) 👇
    $table->boolean('active')->default(false); // default
    $table->string('title'); // ble){
    $table->json('description')->nullable(false)->change(); // 

// Facade way 
Fields::model(Post::class)->eturns ['user_id', 'ulid', 'title', 'description']

class Post extends Model
{
    protected $dispatchesEvents = [
        // a dispatched event trigger listener that fills the `user_id` field
        'creating' => PostCreatingEvent::class, 
    ];

    protected static function boot()
    {
        parent::boot();
        self::observe(PostObserver::class);

        self::creating(function ($model) {
            $model->ulid = Str::ulid();
        });
        
        self::saving(function ($model) {
            $model->title = 'default title'
        });
    }
}

class PostObserver
{
    public function creating(Post $model): void
    {
        $model->description = 'default description';
    }
    
    public function saving(Post $model): void
    {
        $model->description = 'default saving description';
    }
}

Post::eturns [] because it excludes auto-filled fields 

Fields::model(Post::class)->allFields();

// or
Post::allFields();

// returns
// [    'category_id', 'uuid', 'ulid', 'description',
//      'slug', 'created_at', 'updated_at', 'deleted_at'
// ]

Fields::model(Post::class)->nullableFields();

//or
Post::nullableFields();

// returns
// [
//     'category_id', 'uuid', 'slug',
//     'created_at', 'updated_at', 'deleted_at'
// ]

Fields::model(Post::class)->primaryField();

// or
Post::primaryField();

// returns ['id']

Fields::model(Post::class)->databaseDefaultFields();

//or 
Post::databaseDefaultFields();

// returns ['active']

Fields::model(Post::class)->applicationDefaultFields();

//or 
Post::applicationDefaultFields();

// If there are default attributes in the model
class Post extends Model
{
    protected $attributes = [
        'title' => 'default title', 
        'description' => null, // will be ignored
    ];
    
     protected $dispatchesEvents = [
        // if there is a field autofilled by this event,
        // then it will be added to the application default fields
        'creating' => PostCreatingEvent::class, 
    ];
    
    // or any event-filled fields
     protected static function boot(): void
    {
        parent::boot();
        self::observe(PostObserver::class);

        self::creating(function ($model) {
            $model->uuid = Str::uuid();
        });

        self::saving(function ($model) {
            $model->ulid = Str::ulid();
        });
    }
}

// the same in the observer class
class PostObserver
{
    
    public function creating(Post $model): void
    {
        // ..
    }
    
    public function saving(Post $model): void
    {
        // ..
    }
}

// returns
// [
//     'title', 'uuid', 'ulid',
// ]

Fields::model(Post::class)->defaultFields();

//or 
Post::defaultFields();

// This will combine application and database defaults 
class Post extends Model
{
    protected $attributes = [
        'title' => 'default title', 
    ];
    
    protected static function boot(): void
    {
        parent::boot();

        self::creating(function ($model) {
            $model->description = 'default description';
        });
    }
}

// returns
// [
//    'active', 'title', 'description',
// ]
sh
php artisan vendor:publish --provider="WatheqAlshowaiter\ModelFields\ModelFieldsServiceProvider" --tag="config"
sh
php artisan model:fields \\Modules\\Order\\src\\Models\\Order
# or 
php artisan model:fields "Modules\Order\src\Models\Order"
sh
php artisan model:fields User --format=json
php artisan model:fields User --format=table
php artisan model:fields User --format=list  # default