laravel-ready / model-support
Useful model support traits
Installs: 2 575
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/laravel-ready/model-support
Requires
- php: ^8.5 || ^8.4 || ^8.3 || ^8.2 || ^8.1
- illuminate/support: ^12.4 || ^11.8 || ^10.0
Requires (Dev)
- larastan/larastan: ^2.9 || ^3.8
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0
- pestphp/pest: ^2.0 || ^3.0 || ^4.1
- pestphp/pest-plugin-laravel: ^2.0 || ^3.0 || ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.11 || ^2.1
- phpstan/phpstan-deprecation-rules: ^1.2 || ^2.0
- phpstan/phpstan-phpunit: ^1.4 || ^2.0
README
๐ About
Useful eloquent model support traits.
๐ฆ Installation
Get via composer
composer require laravel-ready/model-support
โ๏ธ Configs
php artisan vendor:publish --tag=model-support-config
Example Trait Usage
use LaravelReady\ModelSupport\Traits\Sluggable; use LaravelReady\ModelSupport\Traits\HasActive; ... class Post extends Model { use Sluggable, HasActive; protected $fillable = [ 'title', 'slug', 'content', 'is_active' ]; ... }
๐ Usage
HasLanguage
This trait allows you to get models by language.
Note Field name is
langby default. You can change it in the config file.
use LaravelReady\ModelSupport\Traits\HasLanguage; ... $model->lang('en'); // will return $query->where('lang', $lang); $model->langNot('en'); // will return $query->where('lang', '!=', $lang); $model->langIn(['en', 'tr']); // will return $query->whereIn('lang', $langs); $model->langNotIn(['en', 'tr']); // will return $query->whereNotIn('lang', $langs);
Sluggable
This trait allows you to generate a slug from a string. When you create a new model, the slug will be generated automatically. If you change the title, the slug will also be updated. See bootSluggable() method for more details.
Note Field names are
slugandtitleby default. You can change it in the config file.
use LaravelReady\ModelSupport\Traits\Sluggable; ... $model->slug('any-string'); // will return $query->where('slug', $slug); $model->slugLike('any-string'); // will return $query->where('slug', 'like', $slug); $model->slugNot('any-string'); // will return $query->where('slug', '!=', $slug); $model->slugNotLike('any-string'); // will return $query->where('slug', 'not like', $slug); $model->slugIn(['any-string', 'any-string']); // will return $query->whereIn('slug', $slug); $model->slugNotIn(['any-string', 'any-string']); // will return $query->whereNotIn('slug', $slug);
SluggableTitle
This trait allows you to generate a slug from a title field. Same as Sluggable trait but it only works with the title field.
Note Field names are
slugandtitle(hardcoded, not configurable).
use LaravelReady\ModelSupport\Traits\SluggableTitle; ... $model->slug('any-string'); // will return $query->where('slug', $slug); $model->slugLike('any-string'); // will return $query->where('slug', 'like', $slug); $model->slugNot('any-string'); // will return $query->where('slug', '!=', $slug); $model->slugNotLike('any-string'); // will return $query->where('slug', 'not like', $slug); $model->slugIn(['any-string', 'any-string']); // will return $query->whereIn('slug', $slug); $model->slugNotIn(['any-string', 'any-string']); // will return $query->whereNotIn('slug', $slug);
SluggableName
This trait allows you to generate a slug from a name field. Same as Sluggable trait but it only works with the name field.
Note Field names are
slugandname(hardcoded, not configurable).
use LaravelReady\ModelSupport\Traits\SluggableName; ... $model->slug('any-string'); // will return $query->where('slug', $slug); $model->slugLike('any-string'); // will return $query->where('slug', 'like', $slug); $model->slugNot('any-string'); // will return $query->where('slug', '!=', $slug); $model->slugNotLike('any-string'); // will return $query->where('slug', 'not like', $slug); $model->slugIn(['any-string', 'any-string']); // will return $query->whereIn('slug', $slug); $model->slugNotIn(['any-string', 'any-string']); // will return $query->whereNotIn('slug', $slug);
ParentChild
This trait allows you to work with parent-child relationships in self-referencing models.
Note Field name is
parent_idby default. You can change it in the config file.
Warning It's only supports self-referencing models.
use LaravelReady\ModelSupport\Traits\ParentChild; ... $model->parent(); // will return parent model (BelongsTo relationship) $model->children(); // will return children models (HasMany relationship) $model->recursiveParent(); // will return parent with all recursive parents $model->recursiveChildren(); // will return children with all recursive children $model->recursiveParentAndChildren(); // will return parent and children recursively
HasActive
This trait allows you to get active/inactive status models.
Note Field name is
is_activeby default. You can change it in the config file.
Warning This trait forces your models to fillable
is_activefield and addsis_activecast toboolean.
use LaravelReady\ModelSupport\Traits\HasActive; ... $model->status(true|false); // will return $query->where('is_active', $status); $model->active(); // will return $query->where('is_active', true); $model->inactive(); // will return $query->where('is_active', false);
๐งช Testing
This package uses Pest PHP for testing and is tested across multiple PHP and Laravel versions.
Running Tests
# Run all tests composer test # Run tests with coverage composer test:coverage # Run tests with HTML coverage report composer test:coverage:html
Static Analysis
# Run PHPStan analysis composer test:styles # Run PHPStan with pro features (fix & watch) composer test:styles:pro
Test Matrix
The package is automatically tested against:
| PHP Version | Laravel 10.x | Laravel 11.x | Laravel 12.x |
|---|---|---|---|
| 8.1 | โ | โ | โ |
| 8.2 | โ | โ | โ |
| 8.3 | โ | โ | โ |
| 8.4 | โ | โ | โ |
| 8.5 | โ | โ | โ |
Tests run automatically on every push and pull request via GitHub Actions.
Local Testing
By using act you can run all test matrix locally (requires Docker).
act
โ Credits
- This project was generated by the packager.