1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| use Illuminate\Support\Str; class Item extends Model { public $uid; //设置用户id,根据用户id进行取模(测试而已,正常用户信息可以放到token里,这样全局公用) public function setUid($uid) { $this->uid = $uid; } //重写表名 public function getTable() { $tableName = str_replace('\\', '', Str::snake(Str::pluralStudly(class_basename($this))); //假设当前道具表分成了10个 return $tableName.'_'.($this->uid % 10); //return 'item_'.($this->uid % 10); } //重写数据库连接,提前配置好的,具体看自己的业务,不写就是默认连接 public function getConnection() { return "default"; } }
|