当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Laravel 5.1 - Dry on same characteristics models

问题描述:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: