laravel-ready / model-support
Useful model support traits
Requires
- php: ^8.2 || ^8.1
- illuminate/support: ^11.8 || ^10.0
Requires (Dev)
- mockery/mockery: ^1.6.12
- nunomaduro/larastan: ^v2.9.6
- orchestra/testbench: v9.1.0
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.11.2
- phpstan/phpstan-deprecation-rules: ^1.2.0
- phpstan/phpstan-phpunit: ^1.4.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
lang
by 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
slug
andtitle
by 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.
use LaravelReady\ModelSupport\Traits\SluggableTitle; ...
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.
use LaravelReady\ModelSupport\Traits\SluggableName; ...
ParentChild
This trait allows you to get all children of the model.
Note Field name is
parent_id
by 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 $model->children(); // will return children models $model->allChildren(); // will return all children models $model->allChildrenIds(); // will return all children ids $model->recursiveParentAndChildren(); // will return all parent and children models
HasActive
This trait allows you to get active/inactive status models.
Note Field name is
is_active
by default. You can change it in the config file.
Warning This trait forces your models to fillable
is_active
field and addsis_active
cast 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);
⚓Credits
- This project was generated by the packager.