【问题标题】:Laravel Set Model Table Name With Todays DateLaravel 设置带有今天日期的模型表名称
【发布时间】:2021-01-30 23:10:52
【问题描述】:

我的 laravel 应用程序中有一个模型,它使用每天重新创建的表,并使用包含今天日期的新表名。

我正在尝试将模型中的protected $table 属性设置为

protected $table = "probe_request_" . $this->getDate;

这就是我定义getDate函数的方式

private function getDate()
{
    return Carbon::now('Europe/London')->startOfDay()->format('d_m_Y');
}

我不断收到以下错误"<strong>Zend compile error</strong>: Constant expression contains invalid operations in <strong>/var/www/intelli_sense/app/sprinkles/geo-sense/src/Database/Models/ProbeRequest.php</strong> on line <strong>23</strong>"

有没有一种方法可以设置包含今天日期的表名?我觉得必须有一种我错过的简单方法来做到这一点。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    你可以这样设置新的表名。

    $ProbeRequest= new ProbeRequest;
    $ProbeRequest->setTable("probe_request_" . $this->getDate);
    

    【讨论】:

      【解决方案2】:

      创建字段值时不能使用表达式,但可以在模型构造函数中覆盖它:

      public function __construct(array $attributes = [])
      {
          $this->table = "probe_request_" . Carbon::now('Europe/London')->startOfDay()->format('d_m_Y');
          parent::__construct($attributes);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-05
        相关资源
        最近更新 更多