adev/lrc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

adev/lrc

最新稳定版本:v2.0.3

Composer 安装命令:

composer require adev/lrc

包简介

A package to generate column constants for Laravel models

README 文档

README

A Laravel package to generate column constants for your models based on their table schema. This helps improve consistency and readability in your application, especially when referring to table columns in your code.

Features

Automatically generates constants for each column in your model. Generates a TABLE_NAME constant for each model. Easy to use in migrations, controllers, and other parts of your application.

Installation

You can install the package via Composer. Run the following command in your Laravel project:

composer require adev/lrc

Once installed, the package will automatically register the Artisan command.

Usage

After the package is installed, you can use the make:constants Artisan command to generate column constants for a given model.

Generate Constants for a Single Model To generate constants for a specific model, run:

php artisan make:constants User

This will generate the following constants in your User model (assuming your model is located in app/Models/User.php):

public const TABLE_NAME = 'users';
public const COL_ID = 'id';
public const COL_NAME = 'name';
public const COL_EMAIL = 'email';
// and so on for each column in the 'users' table

Generate Constants for All Models

To generate constants for all models in your app/Models directory, run:

php artisan make:constants all

This will loop through all models and generate column constants for each one.

How to Use the Constants

Once the constants are generated in your models, you can easily use them throughout your application, for example in migrations, controllers, or queries. Here's how you can utilize these constants:

Generate Constants with Fillable

If you want to include a $fillable property in the model using the generated constants, use the --fillable option:

php artisan make:constants User --fillable

Delete Constants

To delete previously generated constants for a specific model, use the --delete option:

php artisan make:constants {model} --delete

For example, to delete constants for the User model:

php artisan make:constants User --delete

To delete constants for all models, use:

php artisan make:constants all --delete

1. In Migrations

When creating or modifying database tables in migrations, you can reference the constants instead of hardcoding column names.

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
   public function up()
   {
           Schema::create('users', function (Blueprint $table) {
           $table->id();
           $table->string('name')->nullable();

           $table->foreignId('family_id')
           ->nullable()
           ->constrained(Family::TABLE_NAME);
           $table->timestamps();
       });
   }

   public function down()
   {
       Schema::dropIfExists(User::TABLE_NAME);
   }
}

2. In Controllers

Using constants makes it easier to refer to column names in your queries or when manipulating data in controllers.

use App\Models\User;

class UserController extends Controller
{
    public function show($id)
    {
        $user = User::where(User::COL_ID, $id)->first();

        if (!$user) {
            return response()->json(['message' => 'User not found'], 404);
        }

        return response()->json($user);
    }

    public function update(Request $request, $id)
    {
        $user = User::where(User::COL_ID, $id)->first();

        if ($user) {
            $user->update([
                User::COL_NAME => $request->name,
                User::COL_EMAIL => $request->email,
            ]);
        }

        return response()->json($user);
    }
}

3. In Seeders

You can also use the constants in seeders to populate the database.

use App\Models\User;

class UserSeeder extends Seeder
{
    public function run()
    {
        User::create([
            User::COL_NAME => 'John Doe',
            User::COL_EMAIL => 'john.doe@example.com',
        ]);
    }
}

4. In Queries

You can use the constants when building queries, which improves readability and reduces the chance of errors due to typos.

use App\Models\User;

// Using constants for column names
$users = User::where(User::COL_NAME, 'like', '%John%')
             ->orderBy(User::COL_EMAIL)
             ->get();

Benefits of Using Constants

Prevents Hardcoding: Instead of hardcoding column names as strings, you can use the constants, which improves maintainability and reduces the chances of errors caused by typos. Improves Readability: Using constants like User::COL_NAME makes it clear that you're referring to a database column, not just any string. Easy Refactoring: If you ever change the column name in the database or the model, you only need to update it in the constants, rather than in every place where the column is referenced in the code. Configuration (Optional) If you'd like to customize the behavior of the package (such as the location of your models or other settings), you can publish the configuration file.

php artisan vendor:publish --provider="LRC\Providers\CommandConstantsServiceProvider" --tag="config"

This will publish the command_constants.php file to your config/ directory, where you can adjust the settings.

License

This package is open-source and available under the MIT License.

Example: Complete Workflow

Let's walk through a complete example where you create a User model, generate column constants, and use them in a migration, controller, and seeder.

1 Create the Model:

If you haven't created the model yet, run:

php artisan make:model User

This will generate a model file at app/Models/User.php.

2 Run the Command to Generate Constants:

After the User model is generated, run the command to generate constants:

php artisan make:constants User

This will add constants like COL_NAME, COL_EMAIL, and TABLE_NAME in the User model.

3 Use Constants in the Migration:

Create a migration to create the users table (if not already done):

php artisan make:migration create_users_table

In the generated migration, use the constants as shown earlier.

4 Use Constants in the Controller:

Now, in the UserController, use these constants to refer to the columns.

5 Use Constants in the Seeder:

You can populate the users table in your UserSeeder using the constants.

Conclusion

By using this package, you can improve the maintainability and readability of your Laravel project by generating and using constants for column names in your models. It ensures that column names are consistent across your application, making it easier to manage as your project grows.

Contact, Support, or Donations

If you would like to contact me for any reason, including support, questions, or contributions to this project, feel free to reach out via:

Email: [adilelkhalloufi@gmail.com] GitHub: https://github.com/adilelkhalloufi

Donations

If you want to support the development of this project, you can make a donation via the following options:

PayPal Any donation is appreciated and will go toward improving this project.

Contributing I welcome contributions! If you'd like to contribute to the project, please follow the instructions in the Contributing Guidelines or simply open an issue or pull request.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-26