mqlo / nameable-cast
This package works best with the mqlo/nameable package for Laravel, Lumen
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 1 159
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- mqlo/nameable: >=2.1.1
README
$nameable->name(), $nameable->value();'name' => $this->name(), 'value' => $this->value().
This package works best with the mqlo/nameable package for Laravel, Lumen.
Example #1
Post
/** * @property PostStatus $status */ class Post extends Model { protected $casts = [ 'status' => PostStatusCast::class, ]; ... }
PostStatus
use Mqlo\Nameable\Nameable; class PostStatus extends Nameable { public const DRAFT = 'draft'; public const PUBLISHED = 'published'; protected static array $all = [ self::DRAFT => 'Draft', self::PUBLISHED => 'Published', ]; public static function draft(): self { return new self(self::DRAFT); } public static function published(): self { return new self(self::PUBLISHED); } public function isDraft(): bool { return $this->name === self::DRAFT; } public function isPublished(): bool { return $this->name === self::PUBLISHED; } }
PostStatusCast
use Mqlo\NameableCast\NameableCast; class PostStatusCast extends NameableCast { protected function nameableClass(): string { return PostStatus::class; } }
Post create
$post = new Post(); $post->status = PostStatus::draft(); $post->save(); //or $post = Post::create(['status' => PostStatus::DRAFT]); //validation $this->validate($request, [ 'title' => 'required|string|max:255', 'status' => 'required|in:' . implode(',', PostStatus::all(false)) ]);
Using
echo $post->status->isDraft() ? 'This post draft.' : '...'; PostStatus::all(true); // with descriptions PostStatus::all(false); // without descriptions return $post; [ ..., 'status' => [ 'name' => 'draft', 'label' => 'Draft' ] ]
Example tables
- tokens
id | type | expire |
---|---|---|
1 | email_confirmation | 2020-10-01 19:00:00 |
2 | password_reset | 2020-10-01 19:30:00 |
... | ... | ... |
- posts
id | title | status |
---|---|---|
1 | Post 1 | draft |
2 | Post 2 | published |
... | ... | ... |