【问题标题】:define table name in laravel model在 laravel 模型中定义表名
【发布时间】:2019-02-26 14:30:15
【问题描述】:

当我在 laravel 模型中定义表名时

class Test extends Model
    {
        use SoftDeletes;
        protected $table="en_test";

并写在我的代码中

DB::enableQueryLog();
Test::get();
dd(DB::getQueryLog());

我的结果不足

"query" => "select * from `en_test` where `en_test`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.88

但是当我设置表名时 受保护的$表;

class Test extends Model
{
    use SoftDeletes;
    public function __construct()
       {
           $locale =\App::getLocale();
           $this->table=$locale."_test";
       }  

并编写以前的代码。我有

"query" => "select * from `en_test`"
    "bindings" => []
    "time" => 0.93

为什么 wherefa_test.deleted_atis null 从我的查询中删除?

【问题讨论】:

    标签: laravel laravel-5 model eloquent localization


    【解决方案1】:

    这是因为您在构造函数中使用了自定义代码,并且您还没有在此处启动父构造函数,因此没有启动特征。你应该使用:

    public function __construct()
    {
       $locale =\App::getLocale();
       $this->table=$locale."_test";
       parent::_construct();
    }
    

    启动父构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2021-11-26
      • 2021-01-12
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      相关资源
      最近更新 更多