PHP code example of krlove / eloquent-model-generator
1. Go to this page and download the library: Download krlove/eloquent-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' );
krlove / eloquent-model-generator example snippets
'providers' => [
Krlove\EloquentModelGenerator\Provider\GeneratorServiceProvider::class,
];
return [
'namespace' => 'App' ,
'base_class_name' => \Illuminate\Database\Eloquent\Model::class,
'output_path' => null ,
'no_timestamps' => null ,
'date_format' => null ,
'connection' => null ,
'no_backup' => null ,
'db_types' => null ,
];
namespace App ;
use Illuminate \Database \Eloquent \Model ;
class User extends Model
{
protected $fillable = ['role_id' , 'username' , 'email' ];
public function role ()
{
return $this ->belongsTo('Role' );
}
public function articles ()
{
return $this ->hasMany('Article' );
}
public function comments ()
{
return $this ->hasMany('Comment' );
}
}
php artisan krlove:generate:models --skip-table=users --skip-table=roles
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], [GeneratorServiceProvider::PROCESSOR_TAG]);
}
protected $perPage = 20 ;
php artisan krlove:generate:model User
php artisan krlove:generate:model User --table-name=user
php artisan krlove:generate:model User --base-class -name =Custom \\Base \\Model
php artisan krlove:generate:model User --no-backup
php artisan krlove:generate:model User