如何在Laravel中通过雄辩的方式将字段添加到模型中? [英] How to add fields to a model via eloquent in Laravel?

查看:41
本文介绍了如何在Laravel中通过雄辩的方式将字段添加到模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,是否可以通过Eloquent将所有字段直接添加到新模型中.

My question is if it is possible to add all the fields directly to a new model via Eloquent.

我想那会是

php artisan make:model MyModel --fields=?

但是,我找不到与此相关的任何内容.无论如何,我必须生成很多模型,任何技巧都将不胜感激.

However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.

预先感谢

推荐答案

如果用 fields 表示表的列,则:

If you mean table's column by fields then:

首先,您不需要在模式中定义 fields .我的意思是在Laravel中,创建模型时无需定义 fields .此外,模型会自动使用数据库表的列作为其属性.

Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.

因此,现在您可能想在创建迁移时定义列,而不是在创建模型时定义列.有一个满足此需求的库称为 Generator ( https://github.com/laracasts/Laravel-5-Generators-Extended )由Laracasts维护.

So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.

使用此生成器,您可以生成迁移文件以在DB中创建表,并指定其列名和类型.以下是他们的Github存储库中的示例,说明了如何执行此操作:

Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:

php工匠make:migration:schema create_users_table --schema =用户名:string,电子邮件:string:unique"

您可以查看他们的文档以获取更多信息.祝你好运.

You can checkout their documentation for more information. Best of luck.

这篇关于如何在Laravel中通过雄辩的方式将字段添加到模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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