Model generator

http://laravel.com/

http://kejyun.github.io/Laravel-4-Docum ... roduction/
回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Model generator

文章 yehlu »

https://github.com/ignasbernotas/larave ... -generator

Model generator
Laravel 5 model generator for an existing schema.

代碼: 選擇全部

composer require ignasbernotas/laravel-model-generator --dev
It plugs into your existing database and generates model class files based on the existing tables.

Installation

代碼: 選擇全部

Add "ignasbernotas/laravel-model-generator": "^1" to your composer.json file.
You'll only want to use these generators for local development, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php, like so:

app/Providers/AppServiceProvider.php

代碼: 選擇全部

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Iber\Generator\ModelGeneratorProvider');
    }
}
Help & Options

代碼: 選擇全部

php artisan help make:models
Options:

--dir="" Model directory (default: "Models/")
--extends="" Parent class (default: "Model")
--fillable="" Rules for $fillable array columns (default: "")
--guarded="" Rules for $guarded array columns (default: "ends:_id|ids,equals:id")
--timestamps="" Rules for $timestamps columns (default: "ends:_at")
--ignore=""|-i="" A table names to ignore
--ignoresystem|-s List of system tables (auth, migrations, entrust package)
--tables="" Tables to generate (E.g.: --tables="my_db_table1,my_db_table2"

Running the generator

代碼: 選擇全部

php artisan make:models
Examples
Table users

SQL

CREATE TABLE `users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(64) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
`email` VARCHAR(45) NULL DEFAULT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Generated Models/Users.php class

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Users extends Model {

public $timestamps = false;

protected $table = 'users';

protected $fillable = ['username', 'email', 'name'];

protected $guarded = ['id', 'password'];

}
Table posts

SQL

CREATE TABLE `posts` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(64) UNSIGNED NOT NULL DEFAULT '',
`content` TEXT NOT NULL DEFAULT '',
`created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Generated Models/Posts.php class

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model {

public $timestamps = true;

protected $table = 'posts';

protected $fillable = ['title', 'content', 'created_at', 'updated_at'];

protected $guarded = ['id'];

}
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Reliese Laravel

文章 yehlu »

https://github.com/reliese/laravel

Reliese Laravel is a collection of Laravel Components which aim is to help the development process of Laravel applications by providing some convenient code-generation capabilities.

How does it work?

This package expects that you are using Laravel 5.1 or above. You will need to import the reliese/laravel package via composer:

composer require reliese/laravel
Configuration

Add the service provider to your config/app.php file within the providers key:

// ...
'providers' => [
/*
* Package Service Providers...
*/

Reliese\Coders\CodersServiceProvider::class,
],
// ...
Configuration for local environment only

If you wish to enable generators only for your local environment, you should install it via composer using the --dev option like this:

composer require reliese/laravel --dev
Then you'll need to register the provider in app/Providers/AppServiceProvider.php file.

public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register(\Reliese\Coders\CodersServiceProvider::class);
}
}
Models

Generating models with artisan

Add the models.php configuration file to your config directory:

php artisan vendor:publish --tag=reliese-models
Usage

Assuming you have already configured your database, you are now all set to go.

Let's scaffold some of your models from your default connection.
php artisan code:models
You can scaffold a specific table like this:
php artisan code:models --table=users
You can also specify the connection:
php artisan code:models --connection=mysql
If you are using a MySQL database, you can specify which schema you want to scaffold:
php artisan code:models --schema=shop
Customizing Model Scaffolding

To change the scaffolding behaviour you can make config/models.php configuration file fit your database needs. Check it out ;-)

Tips

1. Keeping model changes

You may want to generate your models as often as you change your database. In order not to lose you own model changes, you should set base_files to true in your config/models.php.

When you enable this feature your models will inherit their base configurations from base models. You should avoid adding code to your base models, since you will lose all changes when they are generated again.

Note: You will end up with to models for the same table and you may think it is a horrible idea to have two classes for the same thing. However, it is up to you to decide whether this approach gives value to your project :-)
Support

For the time being, this package only supports MySQL databases. Support for other databases will be added soon.
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: Model generator

文章 yehlu »

[Symfony\Component\Debug\Exception\FatalErrorException]
syntax error, unexpected '->' (T_OBJECT_OPERATOR)

代碼: 選擇全部

php composer.phar require ignasbernotas/laravel-model-generator:1.1.*
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: Model generator

文章 yehlu »

https://github.com/reliese/laravel

config/app.php

代碼: 選擇全部

// ...
'providers' => [
    /*
     * Package Service Providers...
     */

    Reliese\Coders\CodersServiceProvider::class,
],
// ...

代碼: 選擇全部

php artisan vendor:publish --tag=reliese-models
php artisan config:clear
php artisan code:models
回覆文章

回到「laravel」