saritasa / eloquent-custom
Saritasa customizations for Eloquent
Installs: 11 906
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 10
Forks: 2
Open Issues: 1
Requires
- php: >=7.0
- illuminate/database: ^5.4 || ^6.0
- illuminate/notifications: ^5.4 || ^6.0
- illuminate/support: 5.* || ^6.0
- saritasa/php-common: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^3.5
README
Custom Extensions for Eloquent
See https://laravel.com/docs/eloquent
Laravel 5.x/6.x
Install the saritasa/eloquent-custom
package:
$ composer require saritasa/eloquent-custom
Optionally (if you want to use default migrations):
If you use Laravel 5.4 or less,
or 5.5+ with package discovery disabled,
add the PredefinedMigrationsServiceProvider service provider config/app.php
:
'providers' => array( // ... Saritasa\Database\Eloquent\PredefinedMigrationsServiceProvider::class, )
then you can execute command:
php artisan vendor:publish --provider=Saritasa\Database\Eloquent\PredefinedMigrationsServiceProvider --tag=migrations
Available classes
Entity
Extends Eloquent model, adds:
- Ability to set default field values for newly created inheritors
Example:
class User extends Entity { protected $defaults = [ 'role' => 'user' ] }
now if you create new user it will have role 'user' by default, if you don't provide it explicitly:
$user = new User(['name' => 'John Doe']); $this->assertEquals('user', $user->role); // true $admin = new User['name' => 'Mary', 'role' => 'admin'); $this->assertEquals('admin', $admin->role); // true
SortByName
Global scope for Eloquent models to add sorting by name by default
Example:
class SomeModel extends Model { ... protected static function boot() { parent::boot(); static::addGlobalScope(new \Saritasa\Database\Eloquent\Scopes\SortByName()); } ... }
CamelCaseModel
Extended class Model for use camel case notation in DB.
Example:
use Saritasa\Database\Eloquent\Models\CamelCaseModel; class SomeModel extends CamelCaseModel { //your code }
CamelCaseForeignKeys trait
Use in any model class for get the default foreign key name for this model.
By default, Eloquent gets foreign keys in snake case notation,
this trait swap notation to camel case like carModelId
instead of car_model_id
.
Use it if you already have foreign keys in camel case notation.
Example:
use Saritasa\Database\Eloquent\Models\CamelCaseForeignKeys; class MyModel extends SomeModelClass { use CamelCaseForeignKeys; }
Contributing
See CONTRIBUTING and Code of Conduct, if you want to make contribution (pull request) or just build and test project on your own.