Skip to content

Commit

Permalink
Add dynamic field name for has active
Browse files Browse the repository at this point in the history
  • Loading branch information
relliv committed Apr 10, 2023
1 parent 674016a commit 1e2955e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ $model->slugNotIn(['any-string', 'any-string']); // will return $query->whereNot

This trait allows you to get all children of the model.

> **Note**
> Field names are `slug` and `title` by default. You can change it in the config file.
> **Warning**
> It's only supports self-referencing models.
Expand All @@ -98,6 +95,9 @@ $model->recursiveParentAndChildren(); // will return all parent and children mod

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 adds `is_active` cast to `boolean`.
Expand Down
13 changes: 13 additions & 0 deletions config/model-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@
*/
'title' => 'title',
],

/**
* HasActive model support configuration
*/
'has_active' => [
/**
* The default is_active field name
*
* @var string
* Ddefault: 'is_active'
*/
'is_active' => 'is_active',
],
];
11 changes: 6 additions & 5 deletions src/Traits/HasActive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

namespace LaravelReady\ModelSupport\Traits;

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Builder;

trait HasActive
{
public function initializeHasActive(): void
{
// append the is_active column to the fillable array
$this->fillable[] = 'is_active';
$this->fillable[] = Config::get('has_active.is_active', 'is_active');

// add the is_active column to the casts array
$this->casts['is_active'] = 'boolean';
$this->casts[Config::get('has_active.is_active', 'is_active')] = 'boolean';
}

public function scopeStatus(mixed $query, bool $status): Builder
{
return $query->where('is_active', $status);
return $query->where(Config::get('has_active.is_active', 'is_active'), $status);
}

public function scopeActive(mixed $query): Builder
{
return $query->where('is_active', true);
return $query->where(Config::get('has_active.is_active', 'is_active'), true);
}

public function scopeInactive(mixed $query): Builder
{
return $query->where('is_active', false);
return $query->where(Config::get('has_active.is_active', 'is_active'), false);
}
}

0 comments on commit 1e2955e

Please sign in to comment.