PHP code example of sourceboat / laravel-enumeration
1. Go to this page and download the library: Download sourceboat/laravel-enumeration 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/ */
namespace App\Models;
use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
use Sourceboat\Enumeration\Casts\Enum;
class User extends Model
{
protected $casts = [
'type' => Enums::class . ':' . UserType::class,
];
}
namespace App\Models;
use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
use Sourceboat\Enumeration\Casts\Enum;
class User extends Model
{
protected $casts = [
'type' => Enums::class . ':' . UserType::class . ',0', // appending the 0 means it is not nullable,
]; // this seems counter intuitive, but thats the way it is recognized as `false`,
} // as `false` is somehow evaluated as `true`.
// For example when the value has been changed manually in the database. Let's say the type is `10`.
$type = $user->type
// Then the following will be the case:
echo $type === UserType::defaultMember(); // "true"
echo $type->value; // "0"
namespace App\Models;
use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $casts = [
'type' => UserType::class,
];
}
php
UserType::Moderator() // Returns an instance of UserType with UserType::Moderator()->value === 1
php
UserType::Subscriber()->config('permissions'); // which return the given array.
UserType::Moderator()->config('permissions', ['thread.foreign.archive']); // you can also define a default value.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.