BoxDetail.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 BoxDetail extends Model
  12. {
  13. protected $table = "box_detail"; //指定表
  14. const CREATED_AT = 'crt_time';
  15. const UPDATED_AT = 'upd_time';
  16. protected $dateFormat = 'U';
  17. protected $guarded = [];
  18. protected $month = [
  19. 1 => '01_03',
  20. 2 => '01_03',
  21. 3 => '01_03',
  22. 4 => '04_06',
  23. 5 => '04_06',
  24. 6 => '04_06',
  25. 7 => '07_09',
  26. 8 => '07_09',
  27. 9 => '07_09',
  28. 10 => '10_12',
  29. 11 => '10_12',
  30. 12 => '10_12',
  31. ];
  32. public function __construct(array $attributes = [])
  33. {
  34. parent::__construct($attributes);
  35. if (isset($attributes['channel']) && $attributes['channel'] > 0) {
  36. $channel = $attributes['channel'];
  37. $month = (int)substr($channel,4,2);
  38. $channel = substr($channel,0,4).$this->month[$month];
  39. $this->setTableById($channel);
  40. }
  41. }
  42. /**
  43. * @param $channel
  44. */
  45. // 动态指定数据表
  46. public function setTableById($channel)
  47. {
  48. if ($channel > 0) {
  49. $tb = $this->table.'_' . (string)$channel;
  50. $this->createTable($tb);
  51. $this->setTable($tb);
  52. }
  53. }
  54. public function createTable($channel){
  55. // $table_name = 'box_detail_' . (string)$channel;
  56. if(!Schema::hasTable($channel)){
  57. DB::update('create table '.$channel.' like box_detail');
  58. }
  59. }
  60. }