1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace App\Model;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- class DeviceData extends Model
- {
- protected $table = "device_data"; //表
- const CREATED_AT = 'crt_time';
- const UPDATED_AT = 'upd_time';
- protected $dateFormat = 'U';
- protected $guarded = [];
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- if (isset($attributes['channel']) && ! empty($attributes['channel'])) {
- $channel = $attributes['channel'];
- $year_month = substr($channel,0,7);
- $year_month = str_replace('-','_',$year_month);
- $this->setTableById($year_month);
- }
- }
- public function setTableById($channel)
- {
- if (! empty($channel)) {
- $tb = $this->table.'_' . (string)$channel;
- $this->createTable($tb);
- $this->setTable($tb);
- }
- }
- //创建表
- private function createTable($table){
- if(! empty($table) && ! Schema::hasTable($table)){
- //执行建表语句
- DB::statement('create table '. $table .' like device_data_for_create');
- }
- }
- public function is_table_isset(){
- if(Schema::hasTable($this->table)) return true;
- return false;
- }
- }
|