Ad
Separate Table For Each Year In Laravel
How can implement tables based on year in Laravel, like Orders2020
, Orders2019
, Orders2018
etc. And how to switch between?
Ad
Answer
I have a model with huge datas, and every month it will generate nearly 10 million+ records, so I split its table horizon too.
The solution is like this below,
In your Order model:
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);
}
}
So you can get the table you want:
$orders2018 = new Order('2018');
$orders2018->where(...)->get();
$orders = Order::where(...)->get(); // will search from the table this year.
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad