Laravel - 扩展模型 [英] Laravel - Extending Model

查看:112
本文介绍了Laravel - 扩展模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个BaseModel类,它从Model扩展。似乎一切都正常,但现在我在保存时遇到问题。我在这个BaseModel中覆盖了save()方法。我只想在保存前向模型添加一些属性。所以我这样做,然后调用 return parent :: save($ options); 。方法签名仍然相同: public function save(array $ options = [])

I've created a BaseModel class, which extends from Model. It seemed like everything was working fine, but now I've run into a problem when saving. I'm overriding the save() method in this BaseModel. I'd just like to add some attributes to the model before saving. So I do that, then call return parent::save($options);. The method signature is still the same: public function save(array $options = []).

执行插入时,它似乎正在抓取表名的BaseModel类的名称(它使用base_models作为表名称),而不是实际的模型正在得救有人遇到过吗?从模型类中扩展的正确方法是什么?

It appears to be grabbing the name of the BaseModel class for the table name when performing the insert (it's using base_models as the table name), rather than the actual model that is being saved. Has anyone run into this before? What is the proper way of extending from the model class?

我最初创建了一些特性来处理一些额外的功能,但是认为只是创建一个更好的主意/ />

I originally created some traits to handle some extra functionality, but thought it would be a better idea to just create a base model and have my models extend from that instead.

推荐答案

在您的模型(扩展基本模型的小孩)中添加表名明确地例如:

In your model (the child one that extends the base model) add the table name explictly for example:

class SomeChildModel extends BaseModel {

    // Manually set the table name
    protected $table = 'table_name';

}

这篇关于Laravel - 扩展模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆