database 映射 Laravel 的现有数据库表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20892978/
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
Map existing Database table for Laravel
提问by Sangoku
I am looking for a way to map existing tables in a project with the Eloquent ORM and use them in code. I use a MySQL database and plan to migrate to MSSQL. Any way points are appreciated.
我正在寻找一种使用 Eloquent ORM 映射项目中现有表并在代码中使用它们的方法。我使用 MySQL 数据库并计划迁移到 MSSQL。任何方式点表示赞赏。
回答by duellsy
You'll have to do this manually.
您必须手动执行此操作。
i.e., create an eloquent model for each of the tables you want access to in your code using eloquent.
即,使用 eloquent 为您要在代码中访问的每个表创建一个 eloquent 模型。
If you don't have timestamps named created_at and updated_at, in your model you can disable those columns.
如果您没有名为 created_at 和 updated_at 的时间戳,则可以在模型中禁用这些列。
Manually
手动
If you have a users table you could 'map' it with a user.phpfile in your models folder like this
如果您有一个用户表,您可以使用user.php模型文件夹中的文件“映射”它,如下所示
class User extends Eloquent {
protected $table = 'users';
public $timestamps = false;
}
Via artisan
通过工匠
You can use Jeffrey Ways Laravel Generatorsto help streamline the initial creation of your models, however you'll still need to make the timestamp modification manually.
您可以使用Jeffrey Ways Laravel Generators来帮助简化模型的初始创建,但是您仍然需要手动修改时间戳。
回答by Dash
This looks like an old post, but it was edited a couple of days ago, so I don't know if the original author is looking for a solution again, but if someone needs this info, here is a packagist package for Laravel 5 to do what you are asking.
这看起来像一个旧帖子,但它是几天前编辑的,所以我不知道原作者是否再次寻找解决方案,但是如果有人需要此信息,这里是 Laravel 5 的 packagist 包做你要问的。
Laravel 5 model generator from existing schema: https://packagist.org/packages/ignasbernotas/laravel-model-generator
来自现有架构的 Laravel 5 模型生成器:https://packagist.org/packages/ignasbernotas/laravel-model-generator
Hope that helps someone!
希望对某人有所帮助!
回答by Andrey Mischenko
There is also a Eloquent Model Generatorlibrary. It can be used for generating Eloquent models using database tables as a source. Generated model will include relation methods, docblocks for magic field and relations and several additional properties.
还有一个Eloquent 模型生成器库。它可用于使用数据库表作为源生成 Eloquent 模型。生成的模型将包括关系方法、魔术字段和关系的文档块以及一些附加属性。
回答by Meetai.com
Another here: https://github.com/Xethron/migrations-generator.
另一个在这里:https: //github.com/Xethron/migrations-generator。
You'll only want to use these generators for local development, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php.
您只想将这些生成器用于本地开发,因此您不想更新 config/app.php 中的生产提供程序数组。相反,在 app/Providers/AppServiceProvider.php 中添加提供程序。
For more details look here - https://packagist.org/packages/ignasbernotas/laravel-model-generator#user-content-installation
有关更多详细信息,请查看此处 - https://packagist.org/packages/ignasbernotas/laravel-model-generator#user-content-installation
回答by Caleb Labeaux
You can also use SQL Server Migration Assistant (SSMA) to port the database to SQL Server, but you will still need to write your own models to match the schema.
您还可以使用 SQL Server 迁移助手 (SSMA) 将数据库移植到 SQL Server,但您仍需要编写自己的模型以匹配架构。
http://blogs.msdn.com/b/ssma/http://www.microsoft.com/en-us/download/details.aspx?id=43688
http://blogs.msdn.com/b/ssma/ http://www.microsoft.com/en-us/download/details.aspx?id=43688
Still this might help get halfway there, from both sides of the puzzle.
尽管如此,从拼图的两边来看,这可能有助于达到一半。

