Skip to content

Instantly share code, notes, and snippets.

@igoralves1
Last active March 4, 2019 19:16
Show Gist options
  • Save igoralves1/739b41ddd87f95d48412522b4e2fbbbd to your computer and use it in GitHub Desktop.
Save igoralves1/739b41ddd87f95d48412522b4e2fbbbd to your computer and use it in GitHub Desktop.
Create migrations and models from existing database - Reliese Laravel
/*
https://github.com/reliese/laravel
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
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
https://medium.com/@CristianLLanos/eloquent-models-from-my-database-5d74c632e03c#.op66eknyk
http://labs.infyom.com/laravelgenerator/
Eloquent: Mutators
https://laravel.com/docs/5.4/eloquent-mutators
===================================================
https://laracasts.com/discuss/channels/laravel/create-migrations-and-models-from-existing-database
If you are using MySQL and Laravel 5.1 or above you can use php artisan code:models from this package reliese/laravel. All you need to do is:
composer require reliese/laravel
Add the service provider to your config/app.php file Reliese\Coders\CodersServiceProvider::class
Publish the config file with php artisan vendor:publish --tag=reliese-models
Make sure your database is correctly configured in config/database.php and .env files.
And finally issue the command: php artisan code:models
This package will scan your database and create all models for you. If you need something more specific, you can customize its config file.
Hope this helps :)
#======================================================================================
# Routes =>calls=>Controllers=>calls=>Models
*/
#Inside Models
<?php
/**
* Created by Reliese Model.
* Date: Mon, 06 Mar 2017 02:14:38 +0000.
*/
namespace App\Models;
use Reliese\Database\Eloquent\Model as Eloquent;
/**
* Class City
*
* @property int $fk_province
* @property int $id
* @property string $name
* @property string $geocodeBr
* @property float $lat
* @property float $long
*
* @property \App\Models\Province $province
* @property \Illuminate\Database\Eloquent\Collection $addresses
* @property \Illuminate\Database\Eloquent\Collection $zona_eleitorals
*
* @package App\Models
*/
class City extends Eloquent
{
protected $table = 'city';
public $timestamps = false;
protected $casts = [
'fk_province' => 'int',
'lat' => 'float',
'long' => 'float'
];
protected $fillable = [
'fk_province',
'name',
'geocodeBr',
'lat',
'long'
];
public function province()
{
return $this->belongsTo(\App\Models\Province::class, 'fk_province');
}
public function addresses()
{
return $this->hasMany(\App\Models\Address::class, 'fk_city');
}
public function zona_eleitorals()
{
return $this->hasMany(\App\Models\ZonaEleitoral::class, 'fk_city');
}
#Note we don`t need to use selct * from Cities. We just use all() method from Eloquent that was inherited
public function allCities() {
return self::all();
}
}
=======================================================================
/*
#Inside Controllers
Creating a Controller based on a Model City. CLI
ila@ig:~/vhosts/drefapi.com(master)$ php artisan make:controller CityController --resource --model=Models\\City
Controller created successfully.
*/
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CityController extends Controller
{
/*
API REST com Laravel => https://www.youtube.com/watch?v=u6a1G7LpWFU&t=1226s
18:50
Using Repository Pattern in Laravel 5
https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/
*/
protected $city=null;
public function __construct(City $city) {
return $this->city=$city;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return $this->city->allCities();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\City $city
* @return \Illuminate\Http\Response
*/
public function show(City $city)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\City $city
* @return \Illuminate\Http\Response
*/
public function edit(City $city)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\City $city
* @return \Illuminate\Http\Response
*/
public function update(Request $request, City $city)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\City $city
* @return \Illuminate\Http\Response
*/
public function destroy(City $city)
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment