generathor-laravel
v1.0.3
Published
Use this to create Eloquent models and CRUDs
Downloads
37
Readme
Generathor Laravel
Generathor Laravel allows you to automatically generate Eloquent models and CRUD operations based on your database structure.
Table of Contents
Installation
To begin using Generathor Laravel, install the necessary dependencies:
$ npm i -D generathor generathor-db generathor-laravel mysql2
Then, create the generathor configuration file:
$ touch generathor.config.cjs
The content of the configuration file will depend on what you want to generate.
Add the following script to your package.json
:
"scripts": {
"generathor": "generathor -c generathor.config.cjs"
}
Generating Files
Whether you need to generate Eloquent models or CRUDs, you must install the PHP dependencies by running the following command:
$ composer require tucker-eric/eloquentfilter kyslik/column-sortable
Next, follow one of the steps below.
Generating Eloquent Models
Your generathor.config.cjs
file should look like this:
const { Source } = require('generathor-db');
const { LaravelGenerator } = require('generathor-laravel');
const laravel = new LaravelGenerator({
createEloquentModelsOnly: true
});
const dbSource = new Source({
type: 'mysql',
configuration: {
host: 'localhost',
port: '3306',
user: 'my_user',
password: 'my_password',
database: 'my_database'
},
excludes: ['migrations']
}, [
laravel.transformer.bind(laravel)
]);
module.exports = {
sources: {
db: dbSource,
},
generators: laravel.generators()
};
Then, run the following command:
$ npm run generathor
Generating Eloquent Models and CRUDs
Your generathor.config.cjs
file should look like this:
const { Source } = require('generathor-db');
const { LaravelGenerator } = require('generathor-laravel');
const laravel = new LaravelGenerator();
const dbSource = new Source({
type: 'mysql',
configuration: {
host: 'localhost',
port: '3306',
user: 'my_user',
password: 'my_password',
database: 'my_database'
},
excludes: ['migrations']
}, [
laravel.transformer.bind(laravel)
]);
module.exports = {
sources: {
db: dbSource,
},
generators: laravel.generators()
};
Then, run the following command:
$ npm run generathor
To make everything work, it's necessary to set up other things in your project:
Installing frontend dependencies
You need to install TailwindCSS: https://tailwindcss.com/docs/guides/laravel
Also, you need to install the following packages:
- alpinejs
- @alpinejs/collapse
- laravel-precognition-alpine
- sweetalert2
- @fortawesome/fontawesome-free
npm i -D alpinejs @alpinejs/collapse laravel-precognition-alpine sweetalert2 @fortawesome/fontawesome-free
File app.js
must contain:
import Alpine from 'alpinejs';
import collapse from '@alpinejs/collapse'
import Precognition from 'laravel-precognition-alpine';
import Swal from 'sweetalert2';
window.Alpine = Alpine;
window.Swal = Swal;
Alpine.plugin(Precognition);
Alpine.plugin(collapse);
Alpine.start();
File app.css
must contain:
@import "@fortawesome/fontawesome-free/css/all.css";
Expose the routes
Add generatehor routes to routes/web.php
:
Route::prefix('manage')->group(function () {
require __DIR__.'/generathor.php';
});
Set up your 'home' route
You neet to set up your home
route. You can change the home route reference in the generathor.config.cjs
file.
Route::get('/', function () {
return view('welcome');
})->name('home');
Set up datepickers (optional)
Install the following package:
npm i -D flatpickr
Add the following code to app.js
:
import flatpickr from 'flatpickr';
flatpickr('.input-datetime', {
enableTime: true,
enableSeconds: true,
dateFormat: 'Y-m-d H:i:S',
});
flatpickr('.input-date', {
dateFormat: 'Y-m-d',
});
flatpickr('.input-time', {
noCalendar: true,
enableTime: true,
enableSeconds: true,
dateFormat: 'H:i:S',
});
Add the following code to app.css
:
@import "flatpickr/dist/flatpickr.css";
Set up loading spinner (optional)
Add the following code to app.js
:
const loader = document.getElementById('generathor-loader');
window.showLoading = () => {
if (loader) {
loader.classList.remove('hidden');
loader.classList.add('flex');
}
};
window.hideLoading = () => {
if (loader) {
loader.classList.remove('flex');
loader.classList.add('hidden');
}
};
const forms = document.getElementsByTagName('form');
for (const form of forms) {
form.addEventListener('submit', window.showLoading);
}
// Only for Livewire
if (typeof Livewire !== undefined) {
Livewire.hook('commit', ({ succeed, fail }) => {
succeed(() => {
window.hideLoading();
});
fail(() => {
window.hideLoading();
});
});
}
Add the following code in layout.blade.php
or in your layout:
<x-generathor.loader />
Set up header (optional)
You can define a header in your layout. For example, in layout.blade.php
or in your layout:
@if(isset($header))
<div>
{{$header}}
</div>
@endif
Additional settings
You can modify settings in the generathor.config.cjs
file.
| Variable | Required | Type | Default | Description |
|----------------------------|----------|------------------------|--------------------------------------|-------------------------------------------------------------------------------------|
| createChildModel
| No
| boolean | true | Prevents overwriting the child class of a model, so you retain your custom changes. |
| createEloquentModelsOnly
| No
| boolean | false | Creates only Eloquent models, skipping the generation of other files. |
| reference
| No
| string | 'laravel-generathor' | Reference name used in templates. |
| source
| No
| string | 'db' | Reference to the Generathor source for database structure. |
| directory
| No
| string | '.' | Directory path for the Laravel project. |
| homeRoute
| No
| string | 'home' | Initial base route, used for redirection to the home page. |
| layout
| No
| string | 'layout' | Main layout of your project. |
| eloquent
| No
| object | | Object for defining parent classes for models. |
| eloquent.parent
| No
| string | 'Illuminate\Database\Eloquent\Model' | General parent class for models. |
| eloquent.customParents
| No
| Record<string, string> | {} | Parent class by table, allowing custom parent classes for specific tables. |
Example
const { Source } = require('generathor-db');
const { LaravelGenerator }= require('generathor-laravel-testing');
const laravel = new LaravelGenerator({
ccreateChildModel: true,
createEloquentModelsOnly: false,
reference: 'laravel',
source: 'db2',
directory: './project',
homeRoute: 'index',
layout: 'app',
eloquent: {
parent: 'App\\Models\\Model',
customParents: {
users: 'App\\Models\\Jetstream\\User as Model',
}
}
});
const dbSource = new Source({
type: 'mysql',
configuration: {
host: 'localhost',
port: '3306',
user: 'my_user',
password: 'my_password',
database: 'my_database'
},
excludes: ['migrations']
}, [
laravel.transformer.bind(laravel)
]);
module.exports = {
sources: {
db: dbSource,
},
generators: laravel.generators()
};
Files
app/Models/Generathor/[model].php
(for each table)app/Models/[model].php
(for each table)app/ModelFilters/[filter].php
(for each table)app/Http/Requests/Generathor/[create-request].php
(for each table)app/Http/Requests/Generathor/[update-request].php
(for each table)app/Http/Requests/Generathor/[filter-request].php
(for each table)app/Http/Requests/Generathor/[attach-request].php
(for each 'has-many' relationship in each table)resources/views/generathor/menu.blade.php
routes/generathor.php
app/Http/Controllers/Generathor/Controller.php
app/Http/Controllers/Generathor/[controller].php
(for each table)resources/views/generathor/[table-context]/index.blade.php
(for each table)resources/views/generathor/[table-context]/edit.blade.php
(for each table)resources/views/generathor/[table-context]/show.blade.php
(for each table)resources/views/generathor/[table-context]/[relation].blade.php
(for each relationship in each table)resources/views/components/generathor/[table-context]/create-form.blade.php
(for each table)resources/views/components/generathor/[table-context]/filter-form.blade.php
(for each table)resources/views/components/generathor/[table-context]/update-form.blade.php
(for each table)resources/views/components/generathor/[table-context]/[attach-form].blade.php
(for each 'has-many' relationship in each table)resources/views/components/generathor/[table-context]/[create-form].blade.php
(for each relationship in each table)resources/views/components/generathor/[table-context]/[filter-form].blade.php
(for each 'has-many' relationship in each table)app/Models/Generathor/GenerathorKey.php
resources/views/components/generathor/record-input.blade.php
resources/views/components/generathor/breadcrumbs.blade.php
resources/views/components/generathor/tabs.blade.php
resources/views/components/generathor/modal.blade.php
resources/views/components/generathor/loader.blade.php
resources/views/components/generathor/icon-check-circle.blade.php
resources/views/components/generathor/icon-chevron-right.blade.php
resources/views/components/generathor/icon-computer.blade.php
resources/views/components/generathor/icon-eye.blade.php
resources/views/components/generathor/icon-funnel.blade.php
resources/views/components/generathor/icon-home.blade.php
resources/views/components/generathor/icon-pencil.blade.php
resources/views/components/generathor/icon-plus.blade.php
resources/views/components/generathor/icon-trash.blade.php
resources/views/components/generathor/icon-x-circle.blade.php
resources/views/components/generathor/icon-x.blade.php
resources/views/components/generathor/icon-chevron-down.blade.php
resources/views/components/generathor/icon-list-bullet.blade.php
resources/views/components/generathor/icon-link.blade.php
resources/views/components/generathor/icon-unlink.blade.php
TODO
- [ ] Separate inputs into components.
- [ ] Customize inputs based on prefix or specific logic.
- [ ] TBD