OrdersProductProcess.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. *
  8. * Class Unit
  9. * @package App\Models
  10. */
  11. class OrdersProductProcess extends Model
  12. {
  13. protected $table = "";
  14. protected $prefix_table = 'orders_product_process'; // 对照表
  15. const CREATED_AT = 'crt_time';
  16. const UPDATED_AT = 'upd_time';
  17. protected $dateFormat = 'U';
  18. //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
  19. //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
  20. protected $fillable = ['param']; // $attributes 里传参数需要配置这个参数名字
  21. public function __construct(array $attributes = [])
  22. {
  23. $param = request()->get('param');
  24. if(! empty($param)) $this->setTableByParam($param);
  25. if(isset($attributes['param']) && $attributes['param'] > 0) $this->setTableByParam($attributes['param']);
  26. $this->createTable($this->table);
  27. parent::__construct($attributes);
  28. }
  29. public function setTableByParam($param){
  30. if($param > 0){
  31. $table = $this->prefix_table . '_' . $param;
  32. $this->table = $table;
  33. }
  34. }
  35. //创建表
  36. private function createTable($table){
  37. if(! empty($table) && ! Schema::hasTable($table) && Schema::hasTable($this->prefix_table)){
  38. //执行建表语句
  39. DB::statement('create table '. $table .' like ' . $this->prefix_table);
  40. }
  41. }
  42. }