Downloads
Stars
Version
Simple generator base on laracademy/generators, extending model generation. Generates model like in Yii framework. Create or update model base on existing table.
Create BaseModel and Model. If there are changes on table schema, regenerating models (by the same command as first generation) process will affect only BaseModel (new rules, fields, etc), so write your logic in Model class to prevent it from overriding.
Also base relations are generated to the models (relation hasOne()
and belongsTo()
).
Column parent_id
will generate relation to itself, available by parent()
Base
$model = new Model();
$model->email = "xxx";
if( !$model->save() )
print_r( $model->getErrors() );
// if validation will fail, e.g. output:
// {"id":["The id field is required."],"email":["The email must be a valid email address."]}
or use model validation in api controller, example store action
public function store(Request $request)
{
$model = new Model();
$model->fill($request->all());
$model->getValidator()->validate();
$model->save();
return response()->json( [ 'message' => __('Success'), 'redirect' => route('model.edit', [$model] ) ] );
}
Example 'BaseModel'
<?php
namespace App\Models\Base;
use Colorgreen\Generator\Models\BaseModel;
/**
* Class BasePage
* @property int id
* @property string image
* @property boolean active
* @property int count
* @property string email
* @property int related_model_id
* @property \App\Models\RelatedModel related_model
*/
class BasePage extends BaseModel
{
protected $attributes = [
'email' => 'default_email',
'active' => 1,
'count' => 0
];
protected static $rules = [
'id' => 'required|numeric|integer',
'image' => 'nullable|string|max:255',
'active' => 'nullable|boolean',
'count' => 'nullable|numeric|integer',
'email' => 'required|string|max:100|email'
];
// samlple hasMany relation: check if relation shouldn't be OneToOne ( hasOne() )
public function pages() {
return $this->hasMany('SmallElectron\Cms\Models\Page', 'redirect_type_id' );
}
public function related_model() {
return $this->belongsTo( App\Models\RelatedModel::class, 'related_model_id' );
}
protected $table = 'pages';
protected $fillable = ['image', 'active', 'count', 'email'];
protected $hidden = [];
protected $casts = [ 'active' => 'boolean', 'count' => 'integer', 'related_model_id' => 'integer' ];
protected $dates = ['created_at', 'updated_at'];
// set in to 'false' if model shouldn't be validated during save.
// you can turn off validation on specific model using $model->setValidation(false)
protected $validation = true;
}
composer require colorgreen/laravel-model-generator
This command will read your database table and generate a model based on that table structure. The fillable fields, casts, dates and even namespacing will be filled in automatically.
You can use this command to generate a single table, multiple tables or all of your tables at once.
MyPage
.config/database.php
config/database.php
first.php artisan cgenerator:modelfromtable --table=users
Will generate model with name User
php artisan cgenerator:modelfromtable --model_name=MyUser --table=users
php artisan cgenerator:modelfromtable --table=users,posts
php artisan cgenerator:modelfromtable --all
php artisan cgenerator:modelfromtable --all --prefix=foo_
For tables: blog_posts, blog_comments, shop_products, users, command php artisan generate:modelfromtable --all --prefix=blog_
will generate models only for blog_posts and blog_comments, model Post
and Comment
database.php
and generating models for all tablesphp artisan cgenerator:modelfromtable --connection=spark --all
php artisan cgenerator:modelfromtable --table=user --folder=app\Models
Based on Laracadeny Generators
Free to use