Initial commit
This commit is contained in:
103
app/Traits/ActiveStatesTrait.php
Normal file
103
app/Traits/ActiveStatesTrait.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait ActiveStatesTrait
|
||||
{
|
||||
/**
|
||||
* Scope a query to only include active (inactive_at collumn) models.
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where(function ($q) {
|
||||
$q->whereNull('inactive_at')
|
||||
->orWhere('inactive_at', '>', now());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include inactive (inactive_at) models.
|
||||
*/
|
||||
public function scopeNotActive($query)
|
||||
{
|
||||
return $query->whereNotNull('inactive_at')
|
||||
->where('inactive_at', '<=', now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate the model (set inactive_at to now).
|
||||
*/
|
||||
public function deactivate()
|
||||
{
|
||||
$this->inactive_at = now();
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reactivate the model (set inactive_at to null).
|
||||
*/
|
||||
public function activate()
|
||||
{
|
||||
$this->inactive_at = null;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scope a query to not include removed (deleted_at) models.
|
||||
*/
|
||||
public function scopeNotRemoved($query)
|
||||
{
|
||||
return $query->where(function ($q) {
|
||||
$q->whereNull('deleted_at')
|
||||
->orWhere('deleted_at', '>', now());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scope a query to only include removed (deleted_at) models.
|
||||
*/
|
||||
public function scopeRemoved($query)
|
||||
{
|
||||
return $query->whereNotNull('deleted_at')
|
||||
->where('deleted_at', '<=', now());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scope a query to only include models with a verified email.
|
||||
*/
|
||||
public function scopeEmailVerified($query)
|
||||
{
|
||||
return $query->whereNotNull('email_verified_at')
|
||||
->where('email_verified_at', '<=', now());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the model has a verified email in the past.
|
||||
*/
|
||||
public function isEmailVerified()
|
||||
{
|
||||
return !is_null($this->email_verified_at) && $this->email_verified_at <= now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the model is currently active (inactive_at).
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return is_null($this->inactive_at) || $this->inactive_at > now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the model is currently removed (deleted_at).
|
||||
*/
|
||||
public function isRemoved()
|
||||
{
|
||||
return !is_null($this->deleted_at) && $this->deleted_at <= now();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user