bozkrewol / modelgenerator
auto generate model class for Laravel illuminate database
dev-master / 1.1.x-dev
2017-08-27 11:13 UTC
Requires
- php: >=5.6.0
- illuminate/database: ^5.1
Suggests
- slim/slim: Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs
- slim/twig-view: Slim Framework 3 view helper built on top of the Twig 2 templating component
This package is not auto-updated.
Last update: 2021-01-09 08:24:16 UTC
README
auto generate model class for illuminate database
Basic Usage
create database connection config
<?php
$config = [
'database' => [
'driver' => "mysql",
'host' => "localhost",
'database' => "yourdatabasename",
'username' => "username",
'password' => "password",
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]
];
// Made a New Connetion.
$manager = new Illuminate\Database\Capsule\Manager();
$manager->addConnection($config["database"]);
$manager->bootEloquent();
// Set as global Connection
$manager->setAsGlobal();
// How to usage
$makeModel = new ModelGenerator\MakeModel($manager);
// Place A new Model Instance
$makeModel->setdirectory("app/model/tables");
// Make a name space
$makeModel->setNamespace("App\\model\\tables");
// Set visibility a new Model Class
$makeModel->setClassvisibility("final");
// Add Hidden Column. ( string $type [ type of column ] ) --optional
$type = "datetime";
$makeModel->enableHidden($type);
// And Make a New Model Instance
// this will generate a new class model, from active database connetion
// will get all table and auto generate it to a new class Model.
$makeModel->create();
?>
Result
<?php
/**
* from database YourDatabase
* table TableName
* generate class table for Model use
*/
namespace App\model\tables;
use Illuminate\Database\Eloquent\Model;
/**
* Class TableClassName
* @package App\model\tables\TableClassName
*/
final class TableClassName extends Model
{
/**
* @var bool
*/
public $incrementing = false;
/**
* @var bool
*/
public $timestamps = false;
/**
* @var string
*/
protected $table = tablename";
/**
* @var string
*/
protected $keyType = "int";
/**
* @var string
*/
protected $primaryKey = "id";
/**
* @var array
*/
protected $fillable = [
"id",
"password",
"email",
"username",
];
/**
* @var array
*/
protected $hidden = [ 'created_at', 'updated_at', 'deleted_at', ];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function id_other_relation()
{
return $this->belongsTo(OtherClassRelation::class, "id");
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function owner_id_other_relation()
{
return $this->hasMany(OtherClassRelation::class, "owner_id", "owner_id");
}
?>
How To Use This Model
see doc Illuminate/Database
How To Install
Create a composer.json file in your project root:
{
"require": {
"bozkrewol/modelgenerator": "dev-master"
}
}
Install via composer:
php composer.phar install
Author
License
MIT Public License