Skip to content

Custom generations

Simple custom generation

Laravel Idea allows to create a custom code generations. They can be created and configured in the Settings(or Preferences) window: Languages & Frameworks | Laravel Idea | Custom Generations. Simple custom generation:

New "Create Service" generation will appear in the Laravel Idea code generations menu

app/Services/FooService.php file will be created with this content:

php
<?php

namespace App\Services;

class FooService
{
}

Complex custom generation

Let's create a new file template for more complex generation. File | Settings (or Preferences) | Editor | File and Code Templates

Choose "Default" scheme to store the template inside your IDE files or "Project" scheme to store template in your projects .idea folder (it can be useful to share templates with team).

Laravel Idea Repository template code:

kotlin
#parse("Laravel Ideal Header.php")
use $MODEL_CLASS;

#parse("Laravel Ideal Class Name.php")
{
    /**
      * Returns $MODEL_CLASS_SINGULAR by id
      * @param $id
      * @return ${MODEL_CLASS_NAME}
      */
    public function get($id) {
        return $MODEL_CLASS_NAME::findOrFail($id);
    }
    
    /**
      * Returns all $MODEL_CLASS_PLURAL
      * @return ${MODEL_CLASS_NAME}[]
      */
    public function all() {
        return $MODEL_CLASS_NAME::all();
    }
#if($SAVE)

    /**
      * Saves $MODEL_CLASS_SINGULAR
      * @param ${MODEL_CLASS_NAME} ${MODEL_CLASS_VARIABLE}
      */
    public function save(${MODEL_CLASS_VARIABLE}) {
        ${MODEL_CLASS_VARIABLE}->save();
    }
#end
}

I'll explain a bit later about all these ${MODEL_CLASS_NAME} and other variables. Create a new custom generation:

The new Create Repository generation will ask 2 parameters:

And create this code:

php
<?php

namespace App\Repositories;

use App\Models\ProjectCategory;

class ProjectCategoryRepository extends Repository
{
    /**
     * Returns project category by id
     * @param $id
     * @return ProjectCategory
     */
    public function get($id)
    {
        return ProjectCategory::findOrFail($id);
    }

    /**
     * Returns all project categories
     * @return ProjectCategory[]
     */
    public function all()
    {
        return ProjectCategory::all();
    }

    /**
     * Saves project category
     * @param ProjectCategory $projectCategory
     */
    public function save($projectCategory)
    {
        $projectCategory->save();
    }
}

Code templates

Templates are processed by Apache Velocity. Documentation.

Custom generation parameters have "Template variable name" option and it's value can be used in the template. If template variable name ends with "_CLASS" Laravel Idea creates a lot of other variables. For example if MODEL_CLASS variable has App\Models\ProjectCategory value these variables with values will be available in the template:

VariableValue
MODEL_CLASSApp\Models\ProjectCategory
MODEL_CLASS_NAMEProjectCategory
MODEL_CLASS_NAME_PLURALProjectCategories
MODEL_CLASS_NAMESPACEApp\Models
MODEL_CLASS_FQN\App\Models\ProjectCategory
MODEL_CLASS_VARIABLE$projectCategory
MODEL_CLASS_VARIABLE_PLURAL$projectCategories
MODEL_CLASS_VARIABLE_SNAKE$project_category
MODEL_CLASS_VARIABLE_SNAKE_PLURAL$project_categories
MODEL_CLASS_FIELDprojectCategory
MODEL_CLASS_FIELD_PLURALprojectCategories
MODEL_CLASS_FIELD_SNAKEproject_category
MODEL_CLASS_FIELD_SNAKE_PLURALproject_categories
MODEL_CLASS_SINGULARproject category
MODEL_CLASS_SINGULAR_UCProject category
MODEL_CLASS_PLURALproject categories
MODEL_CLASS_PLURAL_UCProject categories
MODEL_CLASS_SLUGproject-category
MODEL_CLASS_SLUG_PLURALproject-categories