PHP code example of codelieutenant / laravel-pgenum
1. Go to this page and download the library: Download codelieutenant/laravel-pgenum 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/ */
// Accepts PHP Enums and Strings
\Illuminate\Database\Schema\Blueprint::enumeration(string $name);
public function up(): void
{
// Postgres Enums must be known before Table is created
// This is my, `createEnum` must be seperated and before `Schema::create`
Schema::createEnum(MyEnum::class);
Schema::create('users', function(Blueprint $table) {
$table->id();
$table->enumeration(MyEnum::class);
});
}
public function down(): void
{
Schema::dropEnum(MyEnum::class);
}
use \Illuminate\Database\Eloquent\Model;
use \CodeLieutenant\LaravelPgEnum\Casts\EnumCast;
enum Role : string
{
case Admin = 'admin';
case User = 'user';
}
class User extends Model
{
// For Laravel < 11.0 use $casts property
public function casts(): array
{
return [
// This is supported by default
// as long as PHP Enum is backed by string
'role' => Role::class,
// For non-backed/int backed PHP Enums
// custom converter is needed, this also
// can be applied to string backed enums (but not needed)
// This is due to the limitation of Postgres Enums
// as it
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.