| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | <?phpnamespace App\Model;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Schema;/** * * Class Unit * @package App\Models */class OrdersProductProcess extends Model{    protected  $table = "";    protected $prefix_table = 'orders_product_process'; // 对照表    const CREATED_AT = 'crt_time';    const UPDATED_AT = 'upd_time';    protected $dateFormat = 'U';    //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值    //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值    protected $fillable = ['param']; // $attributes 里传参数需要配置这个参数名字    public function __construct(array $attributes = [])    {        $param = request()->get('param');        if(! empty($param)) $this->setTableByParam($param);        if(isset($attributes['param']) && $attributes['param'] > 0) $this->setTableByParam($attributes['param']);        $this->createTable($this->table);        parent::__construct($attributes);    }    public function setTableByParam($param){        if($param > 0){            $table = $this->prefix_table . '_' . $param;            $this->table = $table;        }    }    //创建表    private function createTable($table){        if(! empty($table) && ! Schema::hasTable($table) && Schema::hasTable($this->prefix_table)){            //执行建表语句            DB::statement('create table '. $table .' like ' . $this->prefix_table);        }    }}
 |