[英]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-a
, table-b
, table-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-x
, table-y
, table-z
)。
I need to do the following:我需要执行以下操作:
table-x
from project-Interface to save some details in it?table-x
DB-B 来保存一些细节吗?table-a
, table-b
and table-c
from project-Interface through migrate command in DB-B?table-a
、 table-b
和table-c
吗?table-x
from project-Interface and use it?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:一些答案:
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 项目或位于不同存储库中的项目进行连接 - 它只了解数据库用户。
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.
我将在下面解释更多。
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:
这将增加以下方面的复杂性:
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:
这将更容易管理:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.