dongxiejie9387 2015-11-19 08: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-19 22: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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部