3 回答

TA貢獻1847條經驗 獲得超7個贊
我有一個包含大量數據的模型,每個月它會生成近 1000 萬條以上的記錄,所以我也拆分了它的表格范圍。
解決方案如下所示,
在您的訂單模型中:
use Illuminate\Support\Carbon;
class Order extends Model
{
protected $table = '';
public function __construct($year='')
{
parent::__construct();
if (empty($year)) {
$tablename = 'orders'.Carbon::now()->year;
} else {
$tablename = 'orders'.$year;
}
$this->setTable($tablename);
}
}
所以你可以得到你想要的表:
$orders2018 = new Order('2018');
$orders2018->where(...)->get();
$orders = Order::where(...)->get(); // will search from the table this year.

TA貢獻1735條經驗 獲得超5個贊
我認為你可以做范圍
例子:
public function scopeYear($query,$year)
{
$this->table = 'orders'.year;
return $query;
}
要從 orders2019 表中獲取,只需使用
Order::year('2019')->get();
要為 orders2019 表創建數據,只需使用
Order::year('2019')->create($data);
- 3 回答
- 0 關注
- 158 瀏覽
添加回答
舉報