dongxiejie9387 2015-11-19 16:25 采纳率: 100%
浏览 49
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dpz1983 2015-11-20 06:59
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog