Automatically generate EloquentModel class of Laravel
For a Laravel project that has just begun, we will use the existing database as it is because it is a project with a migration source.
At that time, it is a work record when automatically generating the Eloquent Model class of Laravel from the existing table
This library has been added (unknown how to read/call it) https://github.com/reliese/laravel
Add library with composer
composer require --dev reliese/laravel
At this time, if killed
is displayed, suspect that you are out of memory.
If you are creating a development environment with Docker, you can deal with it by the method written here.
Correspondence when killed after composer require in Docker https://qiita.com/makies/items/3e1064f17a18df750cad
Add package
Add to AppServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Reliese\Coders\CodersServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// add here
if ($this->app->environment() ==='local') {
$this->app->register(CodersServiceProvider::class);
}
// So far
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
Create configuration file
The configuration file will be generated in config/models.php
.
It seems better to delete config once
php artisan vendor:publish --tag=reliese-models
php artisan config:clear
Model file generation
php artisan code:models
Model file is generated for the table in DB.
It is created under app/Models
by default.
You can specify the output destination and parent class in config/models.php
.
If not generated
If not generated, check if the DB table exists If the DB data is not persisted, restarting the DB container will cause the database to disappear. Please run the migration and then try again.
Generated code
- Class property (= table column)
-Described in doc comment of class with
@property
- $table table name
- $primaryKey primary key
- $incrementing whether to increment the primary key
- timestamps column to update timestamps automatically
- Column to auto cast $casts type
- Columns for automatic conversion to $dates Carbon
- $fillable writable column