php - 为何laravel5的orm中setTable没有效果
PHP中文网
PHP中文网 2017-04-11 08:56:48
[PHP讨论组]
use Illuminate\Database\Eloquent\Model as Eloquent;
class MessageModel extends Eloquent{
    public $table = 'message';
    
    public function getModel($tableName){
    
       return $this->setTable($tableName);
    }
}

报错:

[2016-12-18 18:27:33] production.ERROR: PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'message' doesn't exist in vendor/laravel/framework/src/Illuminate/Database/Connection.php:457

因为消息表示分表的所以想通过一个model操作所有的分表的表

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
ringa_lee

今天发现这个是自己使用的问题因为我使用了create静态方法我们可以看到基类的定义

/**
     * Save a new model and return the instance.
     *
     * @param  array  $attributes
     * @return static
     */
    public static function create(array $attributes = [])
    {
        $model = new static($attributes);

        $model->save();

        return $model;
    }
    

所以由此看出凡是改过表名的model都不能再使用静态方法。只能通过其他方法实现

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号