Larravel Model (model) implements table splitting query - a certain number of table splitting queries according to UID Author: Chuwen Time: 2023-04-28 Classification: Laravel , PHP comment When the business reaches a certain amount, some tables may need to be divided into tables, databases and other methods to speed up database queries, which can be implemented in Larravel as follows ###The code implementation is as follows ```php <? php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Orders extends Model { protected $table = 'orders'; /** *Get sub table name * * @param int $uid *@ param int $count How many users divide a table? By default, 10000 users divide a table * @return string */ public static function getPartitionedTableName(int $uid, int $count = 10000): string { $tableNum = ceil($uid / $count); return $this->table . "_" . (($tableNum - 1) * $count + 1) . "_" . ($tableNum * $count); } public function scopeByUid($query, int $uid) { return $query->from(self::getPartitionedTableName($uid))->where('uid', $uid); } } ``` ###Usage ```php //The table searched is orders_1_10000 Orders::byUid(4)->first(); //The table searched is orders_10001_20000 Orders::byUid(20000)->first(); ```