在 Laravel 5 中从数据库创建模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30560485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Models from database in Laravel 5
提问by Santi Barbat
Is there some way to generate models from databasein Laravel 5?
有没有办法在 Laravel 5 中从数据库生成模型?
The generators package only create an empty model.
generators 包只创建一个空模型。
采纳答案by Andrey Mischenko
There is a library Eloquent Model Generatorwhich aim is creating Eloquent models classes using database tables as a source. It generates not only class stub, but relation methods, docblocks for magic fields, additional properties etc.
有一个库Eloquent Model Generator,其目的是使用数据库表作为源创建 Eloquent 模型类。它不仅生成类存根,还生成关系方法、魔术字段的文档块、附加属性等。
It provides a console command php artisan krlove:generate:model ClassName
for generating one class per one call, but you can create your own command to call this command for each table from your database.
它提供了一个控制台命令,php artisan krlove:generate:model ClassName
用于每次调用生成一个类,但您可以创建自己的命令来为数据库中的每个表调用此命令。
回答by Cristian Alberto Llanos Malca
If you are using MySQL and Laravel 5.1 or above you can use php artisan code:models
from this package: reliese/laravel. All you need to do is:
如果您使用的是 MySQL 和 Laravel 5.1 或更高版本,您可以php artisan code:models
从这个包中使用:relise/laravel。您需要做的就是:
composer require reliese/laravel
- Add the service provider to your
config/app.php
fileReliese\Coders\CodersServiceProvider::class
- Publish the config file with
php artisan vendor:publish --tag=reliese-models
- Make sure your database is correctly configured in
config/database.php
and.env
files. - And finally issue the command:
php artisan code:models
composer require reliese/laravel
- 将服务提供商添加到您的
config/app.php
文件中Reliese\Coders\CodersServiceProvider::class
- 发布配置文件
php artisan vendor:publish --tag=reliese-models
- 确保您的数据库在
config/database.php
和.env
文件中正确配置。 - 最后发出命令:
php artisan code:models
This package will scan your database and create all models for you. If you need something more specific, you can customize its config file.
该软件包将扫描您的数据库并为您创建所有模型。如果你需要更具体的东西,你可以自定义它的配置文件。
Hope this helps :)
希望这可以帮助 :)
回答by Divyesh pal
Easiest Way for creating models from database table is just following few steps of the a composer package as
从数据库表创建模型的最简单方法是遵循 Composer 包的几个步骤,如
composer require laracademy/generators
回答by Bira
php artisan make:model Profile
php artisan make:model Profile
Profile - your table name. The models will not be generated if their is no auto incrementing primary key
个人资料 - 您的表名。如果模型不是自动递增的主键,则不会生成模型