定制 hasanhawary/lookup-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hasanhawary/lookup-manager

Composer 安装命令:

composer require hasanhawary/lookup-manager

包简介

Clean Laravel lookup manager: fetch model lookups and enum lists (app and modules) with no app-coupling.

README 文档

README

Latest Stable Version Total Downloads PHP Version License

A powerful, lightweight, and framework-friendly Laravel package for unified lookup management. Handle dynamic model lookups, automatic enum discovery, and secure config exposure with ease. Perfect for building dynamic frontend filters, dropdowns, and admin panels.

✨ Features

🏗️ Model Lookup

  • Dynamic Fetching: Get Eloquent model data using just table names or class names.
  • Auto-Mapping: Smart name resolution (display_nametitlelabelnamefirst_name + last_name).
  • Custom Name Fields: Use $helpModelName in your models to define custom display fields.
  • Advanced Filtering: Apply Eloquent scopes with parameters and custom search terms.
  • Metadata API: Get a list of all available models with translated names for dynamic UI building.
  • Module Support: Works out of the box with Laravel Modules.
  • Pagination: Built-in support for paginated results.

🔢 Enum Management

  • Auto-Discovery: Automatically finds enums in app/Enum and Modules/*/App/Enum.
  • Standardized Format: Returns consistent key, value, label, snake_key, icon, and extra data.
  • Trait-Powered: EnumMethods trait adds powerful helpers like resolve(), getList(), and values().
  • Custom Methods: Allow specific enum static methods through lookup.enums.allowed_methods.

🔒 Secure Configuration

  • Whitelist Approach: Only expose what you explicitly allow in config/lookup.php.
  • Dot Notation: Fetch nested config keys securely.
  • Safety First: Prevents accidental exposure of sensitive environment variables.

🌍 Localization

  • Translation-Aware: Full support for translating model names and enum labels.
  • Fallback Logic: Gracefully falls back to class names if translations are missing.

📦 Installation

composer require hasanhawary/lookup-manager

The service provider and facade are automatically registered.

⚡ Quick Start

1. Model Lookups

Fetch specific model data with advanced options:

use HasanHawary\LookupManager\Facades\Lookup;

$result = Lookup::getModels([
    'tables' => [
        [
            'name'      => 'users',
            'scopes'    => ['active'],              // Applies scopeActive()
            'extra'     => ['email', 'created_at'], // Additional fields
            'paginate'  => true,
            'per_page'  => 10,
            'search'    => 'John'                   // Search in default name fields
        ],
        [
            'name' => 'roles' // Simple lookup
        ]
    ]
]);

🏷️ Customizing the Display Name ($helpModelName)

By default, the package looks for common name fields (name, title, etc.). You can explicitly define which field(s) should be used as the name in the result by adding a $helpModelName property to your model:

namespace Vendor\Product\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    // Single field
    public static $helpModelName = 'sku';

    // Or multiple fields (concatenated)
    // public static $helpModelName = ['brand', 'model_number'];
}

🔗 Automatic Relation Handling (with)

When you request relations using the with key, the package automatically detects and includes the required foreign keys in the database query. This ensures that Laravel can lazy-load or eager-load the relations correctly without you having to manually add the foreign key to the extra fields.

$result = Lookup::getModels([
    'tables' => [
        [
            'name' => 'posts',
            'with' => ['author', 'category'] // Foreign keys like 'author_id' are handled automatically
        ]
    ]
]);

2. Enum Discovery

Fetch enums from anywhere in your app or modules:

$enums = Lookup::getEnums([
    'enums' => [
        ['name' => 'user.status'],               // Uses your configured enum namespace
        ['name' => 'Vendor\\Product\\Enum\\StatusEnum'] // Fully qualified enum class names also work
    ]
]);

3. Config Exposure

Safely expose frontend-required configs:

// Define whitelist in config/lookup.php
'allowed_configs' => [
    'settings' => ['theme', 'logo_url'],
],

// Fetch via API/Facade
$config = Lookup::getConfigs([
    'configs' => [
        ['name' => 'settings']
    ]
]);

🛠️ The EnumMethods Trait

Unlock the full power of Enums by adding the trait to your Enum classes:

namespace Vendor\Product\Enum;

use HasanHawary\LookupManager\Trait\EnumMethods;

enum StatusEnum: int
{
    use EnumMethods;

    case ACTIVE = 1;
    case INACTIVE = 0;
    
    public static function icons(): array {
        return [self::ACTIVE->value => 'check-circle'];
    }
}

What you get:

  • StatusEnum::getList(): Structured array for dropdowns.
  • StatusEnum::resolve($value): Get the human-readable label for a value.
  • StatusEnum::values(): Get all raw values.
  • StatusEnum::commentFormat(): Quick string for DB migrations.

🌐 HTTP API

The package registers these routes automatically by default:

Method Default path Default route name Controller action
GET /api/help-models models HelpController@models
GET /api/help-enums enums HelpController@enums
GET /api/help-configs config HelpController@config

You do not need to define these routes in the host application. Configure route enablement, prefix, middleware, names, and paths in config/lookup.php.

By default the routes use the api prefix and ['api'] middleware. Add your own auth middleware if these endpoints should require authentication.

If the host project already has a route with the same HTTP method and path, or the same route name, Lookup Manager skips only that package route. This prevents the package from taking over an existing project route. You can avoid collisions explicitly by setting a prefix, custom paths, or a route name prefix:

// config/lookup.php
'routes' => [
    'enabled' => true,
    'prefix' => 'lookup',
    'middleware' => ['api'],
    'name_prefix' => 'lookup.',

    'paths' => [
        'models' => 'help-models',
        'enums' => 'help-enums',
        'config' => 'help-configs',
    ],
],

With that config the prepared routes become:

Method Path Route name
GET /lookup/help-models lookup.models
GET /lookup/help-enums lookup.enums
GET /lookup/help-configs lookup.config

Upgrade Note: Route Prefix

The prepared package routes now use the api prefix by default:

  • /api/help-models
  • /api/help-enums
  • /api/help-configs

If your project still needs the older unprefixed URLs, publish the config and set lookup.routes.prefix to an empty string:

'routes' => [
    'prefix' => '',
],

⚙️ Configuration

Publish the config file to define whitelists and root-excluded models:

php artisan vendor:publish --tag="lookup-manager-config"

Root Excluded Models

Automatically apply scopeExcludeRoot to specific models (like Admin/Role) to prevent exposing "root" system records:

// config/lookup.php
'root_excluded_models' => [
    Vendor\Product\Models\User::class,
],

Application and Module Namespaces

Laravel projects can usually leave models.namespaces, models.paths, enums.namespaces, and enums.paths empty; the package will infer the application namespace and standard Laravel paths when available.

For custom app structures or modules, configure namespaces explicitly:

'models' => [
    'namespaces' => ['Vendor\\Product\\Models'],
    'paths' => [base_path('src/Models')],
],

'enums' => [
    'namespaces' => ['Vendor\\Product\\Enum'],
    'paths' => [base_path('src/Enum')],
],

'modules' => [
    'enabled' => true,
    'path' => 'Modules',
    'namespace' => 'Modules',
    'model_namespace' => 'Domain\\Models',
    'enum_namespace' => 'Domain\\Enum',
],

Plain PHP Usage

The HTTP routes, FormRequests, Eloquent model lookup, and Laravel config lookup are Laravel features. Core enum helpers and config lookup can be used outside a Laravel application when dependencies are provided explicitly. Laravel-only features return empty results or throw validation exceptions instead of relying on host-project helpers.

✅ Compatibility

  • PHP: 8.0+
  • Laravel: 10, 11, 12, 13

📜 License

MIT © Hasan Hawary

hasanhawary/lookup-manager 适用场景与选型建议

hasanhawary/lookup-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 520 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helpers」 「laravel」 「models」 「lookup」 「enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hasanhawary/lookup-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hasanhawary/lookup-manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 hasanhawary/lookup-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 520
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 27
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-21