如何在laravel 5.6中为多个表使用一个模型

时间:2018-03-24 17:56:38

标签: php laravel codeigniter

我正在寻找解决方案,但无法真正理解。我是Laravel的新手,我想要一个关于如何将一个模型用于多个表(如CodeIgniter)的简单说明,如下所示:

Controller myController:

public function shipBuilding()
{
    $data = $this->input->post();

    $response = $this->MyModel->shipbuildingSave($data);

}

public function contact()
{
    $data = $this->input->post();

    $response = $this->MyModel->contactSave($data);

}

Model MyModel:

public function shipbuildingSave($data){
    $this->db->insert('tbl_shipbuilding', $data);
    return $this->db->insert_id();
}

public function contactSave($data){
    $this->db->insert('tbl_contact', $data);
    return $this->db->insert_id();
}

1 个答案:

答案 0 :(得分:1)

这不是模型在Laravel中的工作方式。每个模型应该是一个表的表示。

但是,您可以在启动模型时更改表名称:

  <system.webServer>
    <security>
      <requestFiltering>
          <verbs allowUnlisted="false">
              <add verb="GET" allowed="true" />
              <add verb="POST" allowed="true" />
              <add verb="DELETE" allowed="true" />
              <add verb="PUT" allowed="true" />
          </verbs>
      </requestFiltering>
    </security>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runManagedModulesForWebDavRequests="true">
      <remove name="WebDAVModule" />
      <remove name="TelemetryCorrelationHttpModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

但同样,这可能不适合您的情况。