Laravel 5.1 - 在相同特性模型上干燥

问题描述:

In my Laravel 5.1 App I have a lot of aux Models with the same structure. I was thinking in the posibility of make one model and controller for using all of them, but I cannot figure how to do.

I explain, all the database aux tables have the fields ID and name, and are made for CRUD operations and for filling the forms all over the App.

Is possible to specify the table on the methods implemented by Laravel? I mean, stablish the table on construct, on get(), etc. This would made the work a much more simple if I could do AuxTable::create("sex") or even in requests like $request->auxtable("studies")->get().

Am I explaining?

you can do it with single model like below in Model class there is a method called setTable($table) which can set the table name you want to use so consider below

use Illuminate\Database\Eloquent\Model;
class AuxTable implements Model {
   //other class properties
}

in your controller use the model like below

class SampleController extends BaseController {
      public function index() {
         $model = new AuxTable;
         $model->setTable('sex');
         $model->get();
      }
}

this should do the trick