【问题标题】:How to use one model for multiple tables in laravel 5.6如何在 laravel 5.6 中为多个表使用一个模型
【发布时间】:2018-03-24 17:56:38
【问题描述】:

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

控制器我的控制器:

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

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

}

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

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

}

模型我的模型:

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();
}

【问题讨论】:

  • 我没有看到你在你的 codeigniter 模型中使用多个表??
  • @Jigs1212 我更新了,你又可以看到代码了
  • 我仍然没有看到多个表
  • @Jigs1212 正在从 myModel 调用不同的表,例如使用 save 方法的 tbl_shipbuilding 和 tbl_contact
  • 这在 laravel 中是不可能的。模型应该只有一个表。

标签: php laravel codeigniter


【解决方案1】:

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

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

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'example';

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new AgeScope);

        // Set the $this->table depending on some logic.
    }
}

但同样,对于您的情况可能不建议这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多