PHPackages                             gpapakitsos/laravel-traits - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gpapakitsos/laravel-traits

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gpapakitsos/laravel-traits
==========================

A bundle of some useful Laravel Model/Controller traits

v1.5.0(1y ago)0635↓50%MITPHPPHP ^8.2

Since Oct 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/george-papakitsos/laravel-traits)[ Packagist](https://packagist.org/packages/gpapakitsos/laravel-traits)[ RSS](/packages/gpapakitsos-laravel-traits/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (7)Versions (9)Used By (0)

Laravel Model &amp; Controller Traits
=====================================

[](#laravel-model--controller-traits)

A bundle of some useful Laravel Model &amp; Controller traits.

Requirements
------------

[](#requirements)

- [PHP &gt;= 7.4](https://www.php.net/)
- [Laravel &gt;= 8.0](https://laravel.com/)

Installation
------------

[](#installation)

You can install the package via composer:

### Laravel version &gt;= 12

[](#laravel-version--12)

```
composer require gpapakitsos/laravel-traits
```

### Laravel version &lt;= 11

[](#laravel-version--11)

```
composer require gpapakitsos/laravel-traits "~1.3"
```

The service provider will automatically get registered. Optionally, you may manually add the service provider in your `config/app.php` file:

```
'providers' => [
    // ...
    GPapakitsos\LaravelTraits\TraitsServiceProvider::class,
];
```

You should publish the config file with the following command:

```
php artisan vendor:publish --provider="GPapakitsos\LaravelTraits\TraitsServiceProvider"
```

---

### TimestampsAccessor Trait

[](#timestampsaccessor-trait)

Transforms model’s `created_at` and `updated_at` attributes into a format you can configure in your `config/laraveltraits.php` file.

To enable this feature on a model, you must use the `GPapakitsos\LaravelTraits\TimestampsAccessor` trait.

---

### ModelActive Trait

[](#modelactive-trait)

Some useful methods and scopes to handle model’s `active` or `inactive` state. You can configure the model’s "active" attribute in your `config/laraveltraits.php` file.

To enable this feature on a model, you must use the `GPapakitsos\LaravelTraits\ModelActive` trait.

#### Available methods:

[](#available-methods)

```
/**
 * Checks if model’s state is active
 *
 * @return bool
 */
$model->isActive();
```

```
/**
 * Returns the title of model’s state
 *
 * @return string
 */
$model->getActiveTitle();
```

#### Available scopes:

[](#available-scopes)

```
/**
 * Scope a query to only include active models
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return void
 */
$model->active()->get();
```

```
/**
 * Scope a query to only include inactive models
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return void
 */
$model->notActive()->get();
```

---

### ModelOrdering Trait

[](#modelordering-trait)

Some useful methods to handle model’s `ordering` feature. You can configure the model’s "ordering" attribute in your `config/laraveltraits.php` file.

To enable this feature on a model, you must use the `GPapakitsos\LaravelTraits\ModelOrdering` trait.

#### Available methods:

[](#available-methods-1)

```
/**
 * Returns next available ordering value
 *
 * @param  array  $fieldsAndValues
 * @return int
 *
 * @throws ErrorException
 */
$model::getNewOrdering();
// or
$model::getNewOrdering(['category' => 1]);
```

```
/**
 * Resets ordering
 *
 * @param  array  $fieldsAndValues
 * @return void
 *
 * @throws ErrorException
 */
$model::resetOrdering();
// or
$model::resetOrdering(['category' => 1]);
```

---

### ModelFile Trait

[](#modelfile-trait)

Some useful methods to handle file upload on a model; for example user’s avatar.

To enable this feature on a model, you must use the `GPapakitsos\LaravelTraits\ModelFile` trait and you have to define the following constants:

```
