Laravel Infyom with DT
Author: Alpha Olomi
Hello, my name is Alpha, and I'm part of the Openepsa team. I will show you how to use Infyom Generator to create a CRUD demo in Laravel
1. Create a new laravel application
Using the laravel/installer
laravel new fast-admin
cd fast-admin
Setup Local database using SQlite
Create a SQLite flat file
touch database/database.sqlite
Update the .env
variables to direct to sqlite databse
first comment all DB_*
variables
and add the line
// ...
LOG_LEVEL=debug
DB_CONNECTION=sqlite
// DB_CONNECTION=mysql
// DB_...
Migrate
php artisan mgirate
Scaffold the Authentication and Portal UI
Using a Custom AdminLTE preset from Infyom
composer require infyomlabs/laravel-ui-adminlte
Create login/register and home pages and code
For more info see LaravelUI AdminLTE preset
php artisan ui adminlte --auth
Edit resources/views/layouts/app.blade.php
Update yield
to stack
. This will match other blade files generated later on
@stack('third_party_stylesheets')
@stack('third_party_scripts')
Finally, compile the frontend assets
# compile app.css and app.js
npm install && npm run prod
2. Install Infyom Generator
Intro
Infyom Generator is a Laravel package that helps you generate CRUD operations in your Laravel application. It is a great tool to generate CRUD operations in your Laravel application.
composer require infyomlabs/laravel-generator
composer require infyomlabs/adminlte-templates
composer require doctrine/dbal # Used for reading the database
Misc composer packages
Few more packages are required these will be used by the code generated:
composer require laravelcollective/html
composer require laracasts/flash
php artisan vendor:publish --provider="InfyOm\Generator\InfyOmGeneratorServiceProvider"
A config file config/infyom/laravel_generator.php
will be generated.
Update it as follows:
'options' => [
'softDelete' => false, // line 131
'save_schema_file' => false,
'repository_pattern' => false, // line 139
]
we will create a AppBaseController
to extend out Controllers functionality
touch app/Http/Controllers/AppBaseController.php
Update
<?php
namespace App\Http\Controllers;
class AppBaseController extends Controller
{
//
}
3. Install DataTable
Install composer package
composer require yajra/laravel-datatables
Publish assets for buttons
php artisan vendor:publish --tag=datatables-buttons
Install node packages
yarn add datatables.net-bs5 datatables.net-buttons-bs5
Edit resources/js/bootstrap.js
and add the following:
require("bootstrap");
require("datatables.net-bs5")();
require("datatables.net-buttons-bs5")();
Edit resources/scss/app.scss
and add the following:
@import "~datatables.net-bs5/css/dataTables.bootstrap5.css";
@import "~datatables.net-buttons-bs5/css/buttons.bootstrap5.css";
Compile the assets again
# compile app.css and app.js
npm install && npm run prod
4. Create a migration
We will create a migration for products
table.
In the terminal run:
php artisan make:migration create_products_table --create=products
Update the newly created migration to add 3 more columns ie name, description and price
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->float('price', 8, 2);
$table->timestamps();
});
}
}
migrate
php artisan migrate
5. Scaffolding the Application UI
Finally, using the products
table we just created and migrated, we will scaffold the UI for it.
# To to ensure we use latest configurations
# lets clear the cache on more time
php artisan optimize
php artisan infyom:scaffold Product --fromTable --tableName=products
Files generated
A list of files will be generated and enabling a full CRUD on Products
minor tweaks
Edit resources/views/products/table.blade.php
remove @include('layouts.datatables_css')
and @include('layouts.datatables_js')
Recall we bundled all our frontend assets all together
Open
Start the development server
php artisan serve
Top comments (1)
nice one!!