PHP code example of mahmudul / lara-enum

1. Go to this page and download the library: Download mahmudul/lara-enum 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/ */

    

mahmudul / lara-enum example snippets




namespace App\Enums;

use Mahmudul\LaraEnum\Attributes\Description;
use Mahmudul\LaraEnum\Traits\HasEnumAttributes;

enum Status: string
{
    use HasEnumAttributes;

    #[Description('Draft')]
    case DRAFT = 'draft';

    #[Description('Published')]
    case PUBLISHED = 'published';

    // If no Description is provided, label() falls back to a humanized case name
    case IN_REVIEW = 'in_review';
}



namespace App\Enums;

use Mahmudul\LaraEnum\Attributes\Translatable;
use Mahmudul\LaraEnum\Traits\HasEnumAttributes;

enum PaymentStatus: string
{
    use HasEnumAttributes;

    #[Translatable('enums.payment_status.pending')]
    case PENDING = 'pending';

    #[Translatable('enums.payment_status.paid')]
    case PAID = 'paid';
}

// resources/lang/en/enums.php
return [
    'payment_status' => [
        'pending' => 'Pending',
        'paid' => 'Paid',
    ],
];

// Usage (will call Laravel's __() helper under the hood):
PaymentStatus::PAID->label(); // "Paid"

Status::DRAFT->label(); // "Draft"
Status::IN_REVIEW->label(); // "In Review"

Status::values(); // Illuminate\Support\Collection of ["draft", "published", "in_review"]
Status::values(asArray: true); // ["draft", "published", "in_review"]

Status::asOptions();
/* Collection like:
[
    ['label' => 'Draft', 'value' => 'draft'],
    ['label' => 'Published', 'value' => 'published'],
    ['label' => 'In Review', 'value' => 'in_review'],
]
*/

Status::asOptions(labelKey: 'text', valueKey: 'id', asArray: true);
/* Array like:
[
    ['text' => 'Draft', 'id' => 'draft'],
    ['text' => 'Published', 'id' => 'published'],
    ['text' => 'In Review', 'id' => 'in_review'],
]
*/