Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allows you to use a table prefix with standard Laravel models.

License

NotificationsYou must be signed in to change notification settings

SocolaDaiCa/laravel-table-prefix

Repository files navigation

Latest Version on PackagistBuild StatusGitHub Tests Action StatusQuality ScoreTotal Downloads

Allows you to use a table prefix with standard Laravel models.

Have inspiration from ideas[Proposal] Prefixed Eloquent Models

Installation

You can install the package via composer:

composer require socoladaica/laravel-table-prefix

Usage

Using it inside aPost model would look like this:

<?phpnamespaceApp;useIlluminate\Database\Eloquent\Model;useSocoladaica\LaravelTablePrefix\HasTablePrefix;class Postextends Model{use HasTablePrefix;protected$prefix ='blog_';}

Using it inside aCategoryPost pilot would look like this:

<?phpnamespaceApp;useIlluminate\Database\Eloquent\Relations\Pivot;useSocoladaica\LaravelTablePrefix\HasTablePrefix;class CategoryPostextends Pivot{use HasTablePrefix;protected$prefix ='blog_';}

However, if someone were to use this approach and had many models with a prefix that had to be updated this could prove to be a pain. We can do better by creating another trait (this trait would theoretically exist in user-land code, not in the core), say something likeBlogPrefix:

<?phpnamespaceApp;useSocoladaica\LaravelTablePrefix\HasTablePrefix;trait BlogPrefix{use HasTablePrefix;/**     * The table prefix associated with the model.     *     * @var string     */protected$prefix ='blog_';}

The final model might look something like this:

<?phpnamespaceApp;useIlluminate\Database\Eloquent\Model;class Postextends Model{use BlogPrefix;}

The final pivot might look something like this:

<?phpnamespaceApp;useIlluminate\Database\Eloquent\Relations\Pivot;useSocoladaica\LaravelTablePrefix\HasTablePrefix;class CategoryPostextends Pivot{use BlogPrefix;}

After that you can using it inside a migration would look like this:

class CreateSocolaCmsBlogDatabaseextends Migration{/**     * Run the migrations.     *     * @return void     */publicfunctionup()    {        Schema::create(Post::getTableName(),function (Blueprint$table) {$table->id();$table->timestamps();        });        Schema::create(Category::getTableName(),function (Blueprint$table) {$table->id();$table->timestamps();        });    }/**     * Reverse the migrations.     *     * @return void     */publicfunctiondown()    {        Schema::dropIfExists(Post::getTableName());        Schema::dropIfExists(Category::getTableName());    }}

Testing

composertest

Changelog

Please seeCHANGELOG for more information what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

Security

If you discover any security related issues, please emailSocoladaica@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please seeLicense File for more information.

About

Allows you to use a table prefix with standard Laravel models.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp