简体   繁体   English

Laravel和数据库设计

[英]Laravel and Database Design

I asked this question on Reddit, but received no answer. 我在Reddit上问了这个问题,但没有得到答案。 I thought I would try here, instead. 我以为我会在这里尝试。

I am new to Laravel, and have been going over the documentation and also watching Jeffrey Way's NetTuts videos. 我是Laravel的新手,已经阅读了文档,还观看了Jeffrey Way的NetTuts视频。 However I haven't gotten in too deep just yet, so if this is answered clearly somewhere else, please just point me in that direction. 但是我还没有深入,所以如果在其他地方清楚地回答了这个问题,请指出我的方向。

I come from a CodeIgniter background, and for all projects I have done with it, I typically design my databases in MySQL Workbench. 我来自CodeIgniter背景,对于我完成的所有项目,我通常都在MySQL Workbench中设计数据库。 I also use this to make changes to the schema, and also as a visual map of the database. 我还使用它来更改架构以及数据库的可视化地图。 The MySQL Workbench file generally gets passed around with the other developers via Git. MySQL Workbench文件通常通过Git与其他开发人员一起传递。

Laravel seems to want you to create your tables using migrations, which seems a bit counter intuitive coming from the MySQL Workbench side. Laravel似乎希望您使用迁移来创建表,这似乎有点反感,来自MySQL Workbench。 I understand that migrations act as a version control for the database, which seems pretty nice. 我知道迁移可以充当数据库的版本控制,这看起来非常不错。 However, other than that, I don't quite get the point just yet. 但是,除此之外,我还不太明白这一点。

Can anyone explain to me why I should be building the tables out via the migrations feature of Laravel vs. the way I've been doing it? 谁能向我解释为什么我应该通过Laravel的迁移功能与我的工作方式建立表?

Well, this is a valid concern. 好吧,这是一个有效的担忧。 While Laravel doesn't specifically force you to use it, I can think of a few reasons for its implementation: 尽管Laravel并没有特别强迫您使用它,但我可以想到一些实现它的原因:

  1. Compatibility . 相容性 It works with almost any database. 它适用于几乎所有数据库。 If you ever decide to change from MySQL to PostgreSQL, there's very little to change. 如果您决定从MySQL更改为PostgreSQL,则几乎没有更改。
  2. Version control . 版本控制 As you mentioned yourself, it allows you to have control over what's changed in your database, and provides quick and easy way to update your already running database. 正如您自己提到的那样,它使您可以控制数据库中的更改,并提供快速简便的方法来更新您已在运行的数据库。
  3. Easy of use . 易于使用 It's incredibly easy to call it through the command line. 通过命令行调用它非常容易。 Meaning you won't have any problems creating your database on the server side, through a shell. 意味着通过外壳程序在服务器端创建数据库不会有任何问题。

Laravel is a PHP framework. Laravel是一个PHP框架。 Like other frameworks, such as Zend, your development to deployment timeframe will be significantly reduced, as well as developing within a framework that can be understood by other developers as your projects becomes larger and you need more developers involved in the future. 与Zend等其他框架一样,您的开发到部署的时间框架将大大减少,并且在您的项目变得越来越大且将来需要更多开发人员的情况下,其他开发人员可以理解的框架内进行开发。

Migrations, as part of Laravel, are designed to quickly and easily setup a database scheme without the need to type any MySQL. 作为Laravel的一部分,迁移旨在快速轻松地设置数据库方案,而无需键入任何MySQL。 It also presents the schema correctly. 它还可以正确显示架构。 As your schema exists within a file, you can easily rollback and transport your schema with ease. 由于您的模式存在于文件中,因此您可以轻松地回滚和轻松传输模式。

The main reason Laravel has migrations is because version control. Laravel进行迁移的主要原因是因为版本控制。 When you create a migration the name has a date attached to it. 创建迁移时,名称中会附加一个日期。 I use Laravel myself and I absolutely love migrations. 我自己使用Laravel,而且我绝对喜欢迁移。 Teamed up with the Schema builder it makes my life so much easier not using ancient MySQL methods. 与Schema builder一起使用,无需使用古老的MySQL方法,我的生活变得更加轻松。 Here's some examples of the Schema and migration 这是Schema和迁移的一些示例

Simple Command To Create A New Migration File: 创建新迁移文件的简单命令:

php artisan migrate:make create_users_table

Inside The Users Table Migration File: 在用户表迁移文件中:

Schema::create('users', function($table){
     $table->increments('id'); [will create a increments field with name of ID]
     $table->string('username'); [string field with name of username]
     $table->string('password'); [password is a special  field] 
     $table->text('body'); [string field with name of username]
}); 

At the bottom all you need to add is this: 在底部,您只需添加以下内容:

Schema::drop('users');

Then you run the migrate command: 然后运行迁移命令:

php artisan migrate

It will add all the columns to the table. 它将所有列添加到表中。

if you ever want to take back or add to the database all you need to do is run this 如果您想取回数据库或将其添加到数据库中,则只需运行此命令

php artisan migrate:rollback

[you can add specific commands here to only rollback certain tables] [您可以在此处添加特定命令以仅回滚某些表]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM