PHP code example of watheqalshowaiter / model-required-fields

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


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

use WatheqAlshowaiter\ModelRequiredFields\RequiredFields;

class User extends Model
{
   use RequiredFields;
}

User::getRequiredFields(); // returns ['name', 'email', 'password']

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(); // 

use WatheqAlshowaiter\ModelRequiredFields\RequiredFields;

class Post extends Model
{
   use RequiredFields;
}

Post::getRequiredFields(); // returns ['user_id', 'ulid', 'title', 'description']

// The default parameters, only ithNullables = false,
    $withDefaults = false,
    $withPrimaryKey = false
);
// or
Post::getRequiredFields();
// returns ['user_id', 'ulid', 'title', 'description']

// get getRequiredFields(
    $withNullables = true,
    $withDefaults = false,
    $withPrimaryKey = false
);
// or
Post::getRequiredFields(
    $withNullables = true,
);
// or
Post::getRequiredFields(true);
// or
Post::getRequiredFieldsWithNullables();
// returns ['user_id', 'category_id', 'uuid', 'ulid', 'title', 'description', 'slug', 'created_at', 'updated_at', 'deleted_at']

// get getRequiredFields(
    $withNullables = false,
    $withDefaults = true,
    $withPrimaryKey = false
);
// or
Post::getRequiredFieldsWithDefaults();
// returns ['user_id', 'ulid', 'active', 'title', 'description']

// get getRequiredFields(
    $withNullables = false,
    $withDefaults = false,
    $withPrimaryKey = true
);
// or
Post::getRequiredFieldsWithPrimaryKey();
// returns ['id', 'user_id', 'ulid', 'title', 'description']

// get getRequiredFields(
    $withNullables = true,
    $withDefaults = true,
    $withPrimaryKey = false
);
// or
Post::getRequiredFieldsWithNullablesAndDefaults();
// returns ['user_id', 'category_id', 'uuid', 'ulid', 'active', 'title', 'description', 'slug', 'created_at', 'updated_at', 'deleted_at']

// get getRequiredFields(
    $withNullables = true,
    $withDefaults = false,
    $withPrimaryKey = true
);
// or
Post::getRequiredFieldsWithNullablesAndPrimaryKey();
// returns ['id', 'user_id', 'category_id', 'uuid', 'ulid', 'title', 'description', 'slug', 'created_at', 'updated_at', 'deleted_at']

// get getRequiredFields(
    $withNullables = false,
    $withDefaults = true,
    $withPrimaryKey = true
);
// or
Post::getRequiredFieldsWithDefaultsAndPrimaryKey();
// returns ['id', 'user_id', 'ulid', 'active', 'title', 'description']

// get getRequiredFields(
    $withNullables = true,
    $withDefaults = true,
    $withPrimaryKey = true
);
// or
Post::getAllFields();
// returns ['id', 'user_id', 'category_id', 'uuid', 'ulid', 'active', 'title', 'description', 'slug', 'created_at', 'updated_at', 'deleted_at']