Laravel - 在哪里存储状态(标志)?型号,类或配置文件夹?

Cri*_*ian 19 php mysql status laravel eloquent

我需要在mt项目中广泛使用状态.我需要他们对我的users(active,suspended,等),实体(active,pending_activation,inactive)和我的订阅(active,on_grace_period,not_subscribed,never_subscribed).

到目前为止,我认为最好的方法是将它们存储在数据库中,但我觉得将它们放在其他3个选项中要容易得多.

我还认为我可以将它们存储在我的Eloquent模型中作为常量.例如,我的订阅模型如下所示:

// SubscriptionModel
const SUBSCRIBED_ACTIVE = 1;
const SUBSCRIBED_ON_GRACE_PERIOD = 2;
const NOT_SUBSCRIBED = 3;
const NEVER_SUBSCRIBED = 4;
Run Code Online (Sandbox Code Playgroud)

并检索它们,例如在刀片视图中:

// subscription/index.blade.php
@if($user->subscription->status == /App/SubscriptionModel::SUBSCRIBED_ACTIVE)
    <div>You are subscribed. Thank you</div>
@elseif($user->subscription->status == /App/SubscriptionModel::NEVER_SUBSCRIBED)
    <div>You need to create a subscription before being granted full access!</div>
@elseif(...)
    // and so on
Run Code Online (Sandbox Code Playgroud)

如何做同样但使用配置文件夹并添加一个名为的文件status.php.在视图中访问它将是:

@if($user->subscription->status == Config::get('status.subscription.SUBSCRIBED_ACTIVE'))
<div>You are subscribed. Thank you</div>
@elseif(...)
// etc
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?

另外,等式的另一部分怎么样,意味着状态存储在DB.我是否应该只status为订阅表提供一个列并存储应用程序所指示的内容,或者甚至更好地创建一个单独的表subscription_statusesforeign_key subscription_status_idsubscriptions表中有一个?

Mar*_*ean 20

我倾向于为状态创建一个特定的模型,作为枚举.所以,如果我有一个Event模型,我可能有一个相应的EventStatus模型,如下所示:

class EventStatus
{
    const CANCELLED = 'EventCancelled';
    const POSTPONED = 'EventPostponed';
    const RESCHEDULED = 'EventRescheduled';
    const SCHEDULED = 'EventScheduled';
}
Run Code Online (Sandbox Code Playgroud)

然后我可以做这样的检查:

$event->status == EventStatus::CANCELLED;
Run Code Online (Sandbox Code Playgroud)

我通常也会为我的模型添加便捷方法:

class Event extends Model
{
    public function isCancelled()
    {
        return $this->status == EventStatus::CANCELLED;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于"人性化"字符串,我将拥有一个包含文本字符串的语言文件:

<?php // resources/lang/en/event_status.php

return [
    EventStatus::CANCELLED => 'Cancelled',
    EventStatus::POSTPONED => 'Postponed',
    EventStatus::RESCHEDULED => 'Rescheduled',
    EventStatus::SCHEDULED => 'Scheduled',
];
Run Code Online (Sandbox Code Playgroud)


Eds*_*ior 9

在我的应用程序中,我与 @Martin Bean 类似,只是我不为状态创建单独的类,我将它存储在现有的类/模型中。

我要呼吁usersubscriptionentity一个实体

  • 实体有一个status存在于它的模型和数据库中的表中。
  • 每个模型具有的可能值的常数status一样ACTIVEINACTIVEPENDING,等等,并且这些可为每个型号而变化。
  • 创建处理它类似的方法getStatusLabel()listStatus()isActive()isX(),等。
  • 这些isActive/X()仅在真正必要时创建,也许一个模型有 4 个状态,但您只对一个特定的状态进行比较,所以我只isX()为该状态创建一个。

例子

class User
{
    const STATUS_ACTIVE    = 1;
    const STATUS_SUSPENDED = 2;
    const STATUS_INACTIVE  = 3;

    /**
     * Return list of status codes and labels

     * @return array
     */
    public static function listStatus()
    {
        return [
            self::STATUS_ACTIVE    => 'Active',
            self::STATUS_SUSPENDED => 'Suspended',
            self::STATUS_INACTIVE  => 'Inactive'
        ]
    }

    /**
     * Returns label of actual status

     * @param string
     */
    public function statusLabel()
    {
        $list = self::listStatus();

        // little validation here just in case someone mess things
        // up and there's a ghost status saved in DB
        return isset($list[$this->status]) 
            ? $list[$this->status] 
            : $this->status;
    }

    /**
     * Some actions will happen only if it's active, so I have 
     * this method for making things easier.
     * Other status doesn't have a specific method because
     * I usually don't compare agains them
     * @return Boolean
     */
    public function isActive()
    {
        return $this->status == self::STATUS_ACTIVE;
    }
}
Run Code Online (Sandbox Code Playgroud)


AJR*_*ing 6

我不同意其他答案.您的状态信息应存储在数据库中.设计良好的数据库应该清晰可用,无需应用程序.如果您决定使用此数据库为移动应用程序提供支持,会发生什么?您将从数据库中获取一些信息并仅将其存储在Laravel中,这意味着您必须在移动应用程序中复制该状态列表,并将其保留在两者之间.

这种信息应该存储在数据库中.

选项1

如果你的用户永远只能有一个状态,那么你应该使用一个enum字段的值subscribed,subscribed-grace,not-subscribed,never-subscribed

这在您的观点中同样简单:

@if($user->subscription->status == 'subscribed'
Run Code Online (Sandbox Code Playgroud)

选项2

但是,如果您可能有多种状态,那么几乎可以肯定每个状态都有一个单独的字段,并使用a TINYINT来存储10.

单独的状态表?

除非您预计可能会添加更多状态,否则我无法看到使用单独状态表的充分理由,即使您要添加更多状态,也可以添加新值enum或添加新字段,具体取决于适合的选项.

如果您计划使用除用户之外的数据库中的许多其他表的状态,则状态表将是理想的.

如果您决定更改特定状态的含义,则单独状态表的唯一其他原因是.这意味着您可以重命名状态表中的状态,但用户仍然可以通过它的主键链接到它.使用前两种方法更改状态的含义将涉及对结构的更改.

它实际上取决于您预期如何使用它们,但没有理由不将它们保留在数据库中.