Coder Social home page Coder Social logo

laravel-model-generator's Introduction

Model Generator

Laravel 5 model generator for an existing schema.

It plugs into your existing database and generates model class files based on the existing tables.

Installation

composer require ignasbernotas/laravel-model-generator --dev

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:

<?php

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

Help & Options

php artisan help make:models

Usage:
  make:models [options]

Options:
      --tables[=TABLES]          Comma separated table names to generate
      --prefix[=PREFIX]          Table Prefix [default: DB::getTablePrefix()]
      --dir[=DIR]                Model directory [default: "Models/"]
      --extends[=EXTENDS]        Parent class [default: "Illuminate\Database\Eloquent\Model"]
      --fillable[=FILLABLE]      Rules for $fillable array columns [default: ""]
      --guarded[=GUARDED]        Rules for $guarded array columns [default: "ends:_guarded"]
      --timestamps[=TIMESTAMPS]  Rules for $timestamps columns [default: "ends:_at"]
  -i, --ignore[=IGNORE]          Ignores the tables you define, separated with ,
  -f, --force[=FORCE]            Force override [default: false]
  -s, --ignoresystem             If you want to ignore system tables.
                                              Just type --ignoresystem or -s
  -m, --getset                   Defines if you want to generate set and get methods
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --env[=ENV]                The environment the command should run under.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Build models from existing schema.

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'];

}

laravel-model-generator's People

Contributors

ignasbernotas avatar charlyr avatar estebanprimost avatar jacobsantos avatar joseluisq avatar tomsowerby avatar c-harris avatar hooklife avatar wutherland avatar

Watchers

James Cloos avatar José Cuevas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.