简体   繁体   English

在 Laravel 中使用一个项目的数据库的现有表到另一个项目

[英]Use an existing table of a database of one project to another project in Laravel

I created a separate integration directory/project (let's call it as project-Interface) in Laravel and I created new DB (let's call it as DB-A) and 3 new tables ( table-a , table-b , table-c ) through artisan migrate command and models related to that as well in the solution.我在 Laravel 中创建了一个单独的集成目录/项目(我们称之为 project-Interface),我创建了新的 DB(我们称之为 DB-A)和 3 个新表( table-atable-btable-c )通过artisan migrate命令和与此相关的模型以及解决方案。

Now there is a different project (let's call it as project-Web) in Laravel and it has its own database (let's call it as DB-B) and has its own tables and models created ( table-x , table-y , table-z ).现在 Laravel 中有一个不同的项目(我们称之为 project-Web),它有自己的数据库(我们称之为 DB-B),并创建了自己的表和模型( table-xtable-ytable-z )。

I need to do the following:我需要执行以下操作:

  1. Can I use DB-B of table-x from project-Interface to save some details in it?我可以使用 project-Interface 中table-x DB-B 来保存一些细节吗?
  2. Can I create the tables table-a , table-b and table-c from project-Interface through migrate command in DB-B?我可以通过 DB-B 中的 migrate 命令从 project-Interface 创建表table-atable-btable-c吗?
  3. Can I add some more columns in table-x from project-Interface and use it?我可以在 project-Interface 的table-x添加更多列并使用它吗?

Can I use the existing DB connection of DB-B from the env file of project-Web and use it in env file of Project-Interface?我可以从 project-Web 的env文件中使用 DB-B 的现有 DB 连接,并在 Project-Interface 的env文件中使用它吗?

If yes, will it create a new table in DB-B of project-Web, if I created it from project-Interface?如果是,它是否会在 project-Web 的 DB-B 中创建一个新表,如果我从 project-Interface 创建它? Will the changes reflect there also?这些变化也会反映在那里吗? Also, if I created new columns in table-x from project-Interface, will that column be reflected in project-Web?另外,如果我从 project-Interface 在table-x创建新列,该列会反映在 project-Web 中吗?

So the purpose here is that the DB should be the same and it should be common wherein it should contain all the tables in it and whatever changes I am making from project-Interface or project-Web it should use the DB-B.所以这里的目的是 DB 应该是相同的,它应该是通用的,其中它应该包含其中的所有表,以及我从 project-Interface 或 project-Web 所做的任何更改,它应该使用 DB-B。

Some answers:一些答案:

  1. It seems that you are asking if you can write to database B in project A. The short answer is yes, if you can connect to it then you can write to it.似乎您是在问是否可以写入项目 A 中的数据库 B。简短的答案是肯定的,如果您可以连接到它,那么您就可以写入它。 The database does not know that you are connecting from a different Laravel project or a project sitting in a different repo - it only understands database users.数据库不知道您是从不同的 Laravel 项目或位于不同存储库中的项目进行连接 - 它只了解数据库用户。

  2. You can also run migrations belonging to another project, but based on the information so far, I would advise against this.您也可以运行属于另一个项目的迁移,但根据目前的信息,我建议不要这样做。 I will explain more below.我将在下面解释更多。

  3. Adding columns to a different database sounds like a restatement of question 2, and my answer would be the same.将列添加到不同的数据库听起来像是对问题 2 的重述,我的答案是一样的。

The problem you have with mixing migrations is that the second project (project-Web) is not able to create all its tables.您在混合迁移时遇到的问题是第二个项目 (project-Web) 无法创建其所有表。 This will add complexity to:这将增加以下方面的复杂性:

  • creating a new development environment (eg when adding a new team member to the project)创建新的开发环境(例如,向项目添加新团队成员时)
  • creating automated database tests that need to tear down and recreate the database from scratch for every run创建自动化数据库测试,每次运行都需要从头开始拆除和重新创建数据库
  • creating a new staging/production environment创建新的登台/生产环境

Given that you want some communication between the two projects, I wonder if an HTTP API would be in order?鉴于您希望在两个项目之间进行一些通信,我想知道 HTTP API 是否合适? This will be much easier to manage:这将更容易管理:

  • All your tables are stored in one project, and all your logic is stored here too.您的所有表都存储在一个项目中,您的所有逻辑也存储在这里。 This will simplify migrations enormously (and thus the creation of all environments I mentioned earlier).这将极大地简化迁移(从而创建我之前提到的所有环境)。
  • You can offer a web service in one project to offer read/write services to the other project.您可以在一个项目中提供 Web 服务以向另一个项目提供读/写服务。 This is much easier to test from an automation standpoint: in the API project you can run API/HTTP tests, and in the client project, you can use mocks.从自动化的角度来看,这更容易测试:在 API 项目中,您可以运行 API/HTTP 测试,而在客户端项目中,您可以使用模拟。
  • This approach will allow you to test the projects in isolation in a clean manner, and then do automated integration testing if you wish (eg by deploying both projects to a test server using CI/CD, running the migrations and any seed files, and then running tests on one that causes both projects to work together in the designed fashion).这种方法将允许您以干净的方式单独测试项目,然后根据需要进行自动化集成测试(例如,通过使用 CI/CD 将两个项目部署到测试服务器,运行迁移和任何种子文件,然后在一个使两个项目以设计的方式协同工作的项目上运行测试)。

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

相关问题 我们可以使用两个Laravel项目,一个用于Web服务,另一个用于具有单个数据库的后端管理面板吗? - Can we use two Laravel project one for web services & another for Backend admin panel with single database 来自现有表和项目的laravel身份验证 - laravel authentication from existing table and project 在 laravel 项目帮助上的现有表中添加列 - Add column to existing table on laravel project assistance 在现有项目上安装laravel - Installing laravel on existing project 导入现有的 Laravel 5 项目 - Import existing Laravel 5 project 设置 Laravel 现有项目 - Setup a Laravel existing project 如何将图像从一个项目上传到另一个项目 - How to upload image from one project to another project laravel 在与数据库链接的现有远程 Laravel 项目上本地工作 - Working locally on an existing remote Laravel project linked with a database 我的数据库中有患者信息,现在我想在Laravel Project中以另一种形式访问一个人的信息 - I have patients information in database, now I want to access one's information in another form in Laravel Project 如何在另一台PC和数据库中配置现有的Magento 2.1.7项目? - How to configure an existing Magento 2.1.7 project in another PC and Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM