Miron
Miron

Reputation: 1067

One model to several tables Laravel 5

I have MySQL database tables, of which names are in "yyyymmdd" format. I need to select several tables, for example, from "20150901" to "20150930". I think there might be a way to implement only one model for those tables.

If somebody knows how to do so, please help.

Thanks.

Upvotes: 0

Views: 436

Answers (1)

Vijay Sankhat
Vijay Sankhat

Reputation: 341

immagine that this is your model.

class XYZ extends Eloquent {
  public function __construct($table, array $attributes = array()){
     parent::__construct($attributes);
     $this->table = $table;
   }
}

and you have 7 tables suppose for 1 week. then inside controller loop you can have like

$reportData = array();
foreach($tables as $table) {
    $xyzObj = new XYZ($table);
    $reportData[] =  $xyzObj->get();
    //add your query stuff here like where and fetching records
}

Upvotes: 1

Related Questions