Skip to content

Instantly share code, notes, and snippets.

@alisalehi1380
Last active July 17, 2025 07:47
Show Gist options
  • Save alisalehi1380/0ec5da9cdd3d9d6da535c8f05ff0fb6b to your computer and use it in GitHub Desktop.
Save alisalehi1380/0ec5da9cdd3d9d6da535c8f05ff0fb6b to your computer and use it in GitHub Desktop.

Laravel Code Generation Guidelines

When generating Laravel code, follow these strict patterns:

Migration Files:

Schema::create(\App\Models\Entities::Post->table(), function (Blueprint $table): void {
    $table->id();
    $table->string(\App\Models\Fields\PostFld::TITLE);
    $table->timestamps();
});

Field Constants:

final class UserFld
{
    use \App\Models\Traits\Fields\TimestampFlds;

    public const NAME = 'name';
}

Model Structure:

/**
 * @method static Builder|Post published()
 */
final class Post extends Model
{
    use \App\Models\Getters\PostFldGttr;

    public const AUTHOR_REL = 'author';
    
    public function author(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Author::class);
    }
    
    public function scopePublished(Builder $query): void
    {
        $query->where(\App\Models\Fields\PostFld::STATUS, \App\Enums\PostStatuses::Published);
    }
}

Getter Traits:

/**
 * @mixin \App\Models\Post
 */
trait PostFldGttr
{
    use \App\Models\Traits\Getters\TimestampFldsGttr;
    
    public function getTitle(): string
    {
        return $this->getAttribute(\App\Models\Fields\PostFld::TITLE);
    }
}

Filament Resource Structure:

use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Table;
use Filament\Forms\Form;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;

final class PostResource extends \App\Support\Filament\Resource\Resource
{
    public static function table(Table $table): Table
    {
        return $table
            ->columns(self::tableSchema())
            ->filters(self::tableFilters())
            ->actions([
                EditAction::make()
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }


    public static function tableSchema(): array
    {
        return [
            TextColumn::make(PostFld::TITLE)
                ->label('عنوان')
                ->searchable(),
            
            ...self::addTimestamps()
        ];
    }

    public static function tableFilters(): array
    {
        return [
            ...self::datetimeRangeFilter(),
        ];
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make(PostFld::TITLE)
                    ->label('عنوان')
                    ->maxLength(255)
                    ->required(),
            ]);
    }

Requirements:

Always use field constants from \App\Models\Fields\ classes
Include proper PHPDoc annotations for scopes and mixins
Use final classes for models and resources
Include proper return types for all methods
Use relationship constants (e.g., AUTHOR_REL) for relationship names
Generate corresponding Filament resources when creating models
Use proper MySQL column types in migrations
Include timestamps in migrations unless specified otherwise
Follow Persian labels in Filament forms/tables when appropriate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment