PHP code example of thefeqy / laravel-model-status

1. Go to this page and download the library: Download thefeqy/laravel-model-status library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thefeqy / laravel-model-status example snippets


return [
    'column_name' => env('MODEL_STATUS_COLUMN', 'status'),
    'default_value' => env('MODEL_STATUS_ACTIVE', 'active'),
    'inactive_value' => env('MODEL_STATUS_INACTIVE', 'inactive'),

    'admin_detector' => function () {
        return auth()->check() && auth()->user()->is_admin;
    },
];

use Thefeqy\ModelStatus\Traits\HasActiveScope;

class Product extends Model
{
    use HasActiveScope;

    protected $fillable = ['name'];
}

$activeProducts = Product::all(); // Returns only active products

$allProducts = Product::inActive()->get();

$product = Product::find(1);

$product->activate(); // Set status to "active"
$product->deactivate(); // Set status to "inactive"

if ($product->status->isActive()) {
    echo "Product is active!";
}

if ($product->status->isInactive()) {
    echo "Product is inactive!";
}

use Thefeqy\ModelStatus\Casts\StatusCast;

class Product extends Model
{
    use HasActiveScope;

    protected $fillable = ['name', 'status', 'category_id'];

    protected $casts = [
        'status' => StatusCast::class,
    ];
}

$product = Product::find(1);

if ($product->status->isActive()) {
    echo "Product is active!";
}

class Category extends Model
{
    use HasActiveScope;

    protected $fillable = ['name', 'status'];

    protected array $cascadeDeactivate = ['products'];

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

$category = Category::find(1);
$category->deactivate();

use Illuminate\Support\Facades\Route;
use Thefeqy\ModelStatus\Middleware\EnsureAuthenticatedUserIsActive;

Route::middleware(['auth', EnsureAuthenticatedUserIsActive::class])->group(function () {
    Route::get('/dashboard', function () {
        return 'Welcome to your dashboard!';
    });
});

'admin_detector' => function () {
    return auth()->check() && auth()->user()->is_admin;
},

$table->string('state', 10)->default('enabled');

$table->string('status', 10)->default('active');
bash
php artisan model-status:install