Getting started with Infyom Generator on Laravel 9
Tags: webdev, laravel
Published: 2022-06-02 13:41:36

# Laravel Infyom with DT

Author: Alpha Olomi

Hello, my name is Alpha, and I'm part of the [Openepsa team](https://github.com/openpesa/). 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](https://laravel.com/docs/9.x/installation#the-laravel-installer)

```sh
laravel new fast-admin
cd fast-admin
```


**Setup Local database using SQlite**

Create a SQLite flat file
```sh
touch database/database.sqlite
```

Update the `.env` variables to direct to sqlite databse

first comment all `DB_*` variables

and add the line

```ini
// ...
LOG_LEVEL=debug

DB_CONNECTION=sqlite
// DB_CONNECTION=mysql
// DB_...
```


**Migrate**

```sh
php artisan mgirate
```

**Scaffold the Authentication and Portal UI**

Using a Custom [AdminLTE preset](https://github.com/InfyOmLabs/laravel-ui-adminlte) from Infyom

```sh
composer require infyomlabs/laravel-ui-adminlte
```

Create login/register and home pages and code

For more info see [LaravelUI AdminLTE preset](https://infyom.com/open-source/laravel-ui-adminlte/docs)
```sh
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

```blade
@stack('third_party_stylesheets')
```


```blade
@stack('third_party_scripts')
```

Finally, compile the frontend assets

```sh
# 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.


```sh
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:

```sh
composer require laravelcollective/html
composer require laracasts/flash
```

```sh
php artisan vendor:publish --provider="InfyOm\Generator\InfyOmGeneratorServiceProvider"
```

A config file `config/infyom/laravel_generator.php`  will be generated.

Update it as follows:

```php
'options' => [
    'softDelete' => false, // line 131
    'save_schema_file' => false,
    'repository_pattern' => false, // line 139
]
```

we will create a `AppBaseController` to extend out Controllers functionality

```sh
touch app/Http/Controllers/AppBaseController.php
```

Update

```php
<?php

namespace App\Http\Controllers;

class AppBaseController extends Controller
{
    // 
}
```

## 3. Install DataTable 

**Install composer  package**

```sh
composer require yajra/laravel-datatables
```

**Publish assets for buttons**

```sh
php artisan vendor:publish --tag=datatables-buttons
```

**Install node packages**

```sh
yarn add datatables.net-bs5 datatables.net-buttons-bs5
```

Edit `resources/js/bootstrap.js` and add the following:

```js
require("bootstrap");
require("datatables.net-bs5")();
require("datatables.net-buttons-bs5")();
```

Edit `resources/scss/app.scss` and add the following:

```css
@import "~datatables.net-bs5/css/dataTables.bootstrap5.css";
@import "~datatables.net-buttons-bs5/css/buttons.bootstrap5.css";
```

Compile the assets again

```sh
# 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:

```sh
php artisan make:migration create_products_table --create=products
```

Update the newly created migration to add 3 more columns ie name, description and price

```php
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

```sh
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.

```sh
# To to ensure we use latest configurations
# lets clear the cache on more time
php artisan optimize
```

```sh
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

```sh
php artisan serve
```

## Happy Coding!