Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4
Allows you to use a table prefix with standard Laravel models.
License
SocolaDaiCa/laravel-table-prefix
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Allows you to use a table prefix with standard Laravel models.
Have inspiration from ideas[Proposal] Prefixed Eloquent Models
You can install the package via composer:
composer require socoladaica/laravel-table-prefix
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()); }}
composertestPlease seeCHANGELOG for more information what has changed recently.
Please seeCONTRIBUTING for details.
If you discover any security related issues, please emailSocoladaica@gmail.com instead of using the issue tracker.
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.