lundi 8 juillet 2019

Hot to dynamically change $table name is Laravel Model

i'm trying to pull data from a table which keeps changing its suffix based on date. for example Report201906 for June, Report201907 for July.

So i want to connect to a table with the current date suffix. e.g current month is July. so i want to connect to Report201907

I tried

protected $connection = 'mysql2';
protected $primaryKey = 'id';
protected $current_month = date('Ym',strtotime(date('Y-m')." -1 month"));
protected $table = 'Report'.$current_month; //Hoping to connect to Report201907

But i get the Error Constant expression contains invalid operations. I'm pretty new to laravel and PHP.Your help will be highly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire