PHP code example of maheralyamany / laravel-model-generator

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

    

maheralyamany / laravel-model-generator example snippets




public function register()
    {
        if ($this->app->environment() === 'local' && class_exists(\ModelGenerator\ModelGeneratorServiceProvider::class)) {
            $this->app->register(\ModelGenerator\ModelGeneratorServiceProvider::class);
        }
    }



 /**
     * create or update model
     *  @param \Illuminate\Http\Request|object|\stdClass $request
     * @param static $item
     * @return array
     */
    public static function createOrUpdate($request,  $item =null)
    {
        try {
            if (is_null($item)) {
                $item = static::Create([
                    'name' => $request->name ,
                    'username' => $request->username,
                    'email' => $request->email,
                    'role_id' => $request->role_id,
                ]);
            } else {
                 $item->name = $request->name;
                 $item->username = $request->username;
                 $item->email = $request->email;
                 $item->role_id = $request->role_id;
                 $item->save();
            }
            return ["status" => true];
        } catch (\Exception $ex) {
            return  ["status" => false];
        }
    }



return [
    'output_path' => app_path('Models'),
    'namespace' => 'App\Models',
    'base_class_name' => \Illuminate\Database\Eloquent\Model::class,
    'no_timestamps' => false,
    'date_format' => 'Y-m-d H:i:s',
    'connection' => null,
    'has_backup' => null,
    'db_types' => [],
    'except' => [
      'migrations',
      'personal_access_tokens',
      ],
];

return [
    // ...
    'db_types' => [
        '<TYPE>' => 'string',
    ],
];



namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property int $id
 * @property int $role_id
 * @property mixed $username
 * @property mixed $email
 * @property Role $role
 * @property Article[] $articles
 * @property Comment[] $comments
 */
class User extends Model
{
  /**
   * @var array
   */
  protected $fillable = ['role_id', 'username', 'email'];

  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function role()
  {
    return $this->belongsTo('Role');
  }

  /**
   * @return \Illuminate\Database\Eloquent\Relations\HasMany
   */
  public function articles()
  {
    return $this->hasMany('Article');
  }

  /**
   * @return \Illuminate\Database\Eloquent\Relations\HasMany
   */
  public function comments()
  {
    return $this->hasMany('Comment');
  }
}

class PerPageProcessor implements ProcessorInterface
{
  public function process(EloquentModel $model, Config $config): void
  {
    $propertyModel = new PropertyModel('perPage', 'protected', 20);
    $dockBlockModel = new DocBlockModel(
      'The number of models to return for pagination.',
      '',
      '@var int'
    );
    $propertyModel->setDocBlock($dockBlockModel);
    $model->addProperty($propertyModel);
  }

  public function getPriority(): int
  {
    return 8;
  }
}

public function register()
{
    $this->app->tag([InflectorRulesProcessor::class], [ModelGeneratorServiceProvider::PROCESSOR_TAG]);
}

/**
 * The number of models to return for pagination.
 *
 * @var int
 */
protected $perPage = 20;
bash
php artisan maheralyamany:generate:model cache --class-name=Cache
bash
php artisan maheralyamany:generate:models  --allow-tables='table_name'  --has-create='true' 

php artisan maheralyamany:generate:models  --allow-tables='table_name'  --has-create='true' 

php artisan maheralyamany:generate:models   --has-create='false' 

bash
php artisan maheralyamany:generate:model User --base-class-name=Custom\\Base\\Model
bash
php artisan maheralyamany:generate:model User --has-backup=true
bash
php artisan maheralyamany:generate:model User