php - Laravel - Where to store statuses (flags)? Model, Class or config folder? -
i need extensively use statuses in mt project. need them users
(active
, suspended
, etc), entity (active
, pending_activation
, inactive
) , subscriptions(active
, on_grace_period
, not_subscribed
, never_subscribed
).
so far thought best way store them in db have feeling it's easier have them in other 3 options.
i thought can store them in eloquent
model constants. example subscription model this:
// subscriptionmodel const subscribed_active = 1; const subscribed_on_grace_period = 2; const not_subscribed = 3; const never_subscribed = 4;
and retrieving them, example in blade view:
// subscription/index.blade.php @if($user->subscription->status == /app/subscriptionmodel::subscribed_active) <div>you subscribed. thank you</div> @elseif($user->subscription->status == /app/subscriptionmodel::never_subscribed) <div>you need create subscription before being granted full access!</div> @elseif(...) // , on
how doing same using config folder , adding file called status.php
. accessing in view like:
@if($user->subscription->status == config::get('status.subscription.subscribed_active')) <div>you subscribed. thank you</div> @elseif(...) // etc
is there better way?
also, how other part of equation, meaning status stored in db
. should have status
column subscription table , store app dictates or bettter create separate table subscription_statuses
, have foreign_key
subscription_status_id
in subscriptions
table?
i tend create specific model statuses, acts enum. if have event
model, may have corresponding eventstatus
model looks this:
class eventstatus { const cancelled = 'eventcancelled'; const postponed = 'eventpostponed'; const rescheduled = 'eventrescheduled'; const scheduled = 'eventscheduled'; }
i can checks this:
$event->status == eventstatus::cancelled;
and i’ll add convenience methods models too:
class event extends model { public function iscancelled() { return $this->status == eventstatus::cancelled; } }
for “human-friendly” strings, i’ll have language file has text strings:
<?php // resources/lang/en/event_status.php return [ eventstatus::cancelled => 'cancelled'; eventstatus::postponed => 'postponed'; eventstatus::rescheduled => 'rescheduled'; eventstatus::scheduled => 'scheduled'; ];
Comments
Post a Comment