gpt4 book ai didi

php - Laravel 5.1 从 Controller 创建表

转载 作者:行者123 更新时间:2023-11-29 21:47:20 38 4
gpt4 key购买 nike

情况是这样的:我有一个用户列表,该用户与一个公司关联,而这些公司与一个组关联。每个用户可以创建一个与该用户和该用户所属公司相关联的客户。目前,我的所有客户端都位于同一个数据库表中。但我想为每家公司都有一个客户表。

我正在寻找在创建公司时创建客户表的正确方法。例如,当我创建公司 A 时, Controller 在公司表中添加行 A,然后检索 company_id 并创建 client_id 表。现在,我尝试了这个:

    public function storeCompany(Request $request)
{
$company= new Company;
$company-> group_id = Request::get('group_id');
$company-> company_name = Request::get('company_name');
$company-> phone = Request::get('phone');
$company-> save();
$company_id = DB::table('companies')->where('company_name',$company->company_name)->first();
Schema::create('clients_'.$company_id, function($table) {
$table->increments('id');
$table->string('columntest');
});
return redirect('admin/managecompany');
}

我添加了这个:

use Illuminate\Database\Schema\Blueprint;

但是我收到了这个错误:

Class 'App\Http\Controllers\Admin\Schema' not found

最后,我试图找到我的错误的问题,我想知道是否有一个选项可以使它更容易或有任何其他建议以更好的方式做到这一点。

谢谢!

最佳答案

有人放了这个并删除了他的评论:

use Illuminate\Support\Facades\Schema as Schema;

@Tim Lewis 也这么说。这解决了我的问题。但当我测试它时,我遇到了另一个问题。我没有创建模型,因此无法使用

添加行
$client = new Client;

这就是我所做的:

首先,我将函数 storeCompany 更改为:

    public function storeCompany(Request $request)
{
$company = new Company;
$company -> group_id = Request::get('group_id');
$company -> company_name = Request::get('company_name');
$company -> phone = Request::get('phone');
$company -> save();

$company = DB::table('companys')->where('company_name',$company->company_name)->first();
$company_id = $company -> id;

Log::info('Lancement de la création des tables @ ' . \Carbon\Carbon::now());
for ($i=1; $i <6; $i++) {
if ($i == 1 ) {$out = shell_exec('cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\newCompanyTable.bat" "D0" "D'.$company_id.'"');}
if ($i == 2 ) {$out = shell_exec('cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\newCompanyTable.bat" "d0" "d'.$company_id.'"');}
if ($i == 3 ) {$out = shell_exec('C:\xampp\htdocs\site\lastaction\newCompanyTable.bat');}
if ($i == 4 ) {$out = shell_exec('cd C:\xampp\htdocs\site\lastaction && php artisan migrate');}
if ($i == 5 ) {$out = shell_exec('cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\newCompanyTable.bat" "D'.$company_id.'" "D0"');}
if ($i == 6 ) {$out = shell_exec('cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\newCompanyTable.bat" "d'.$company_id.'" "d0"');}
Log::info($out);
}
return redirect('admin/managecompany');
}

其次,我创建了这个 .bat 文件:

cd C:\xampp\htdocs\site\lastaction\app && copy "Client.php" "D0client.php"
cd C:\xampp\htdocs\site\lastaction\app && copy "Transaction.php" "D0transaction.php"
cd C:\xampp\htdocs\site\lastaction\app && copy "Job.php" "D0job.php"
cd C:\xampp\htdocs\site\lastaction\app && copy "Log.php" "D0log.php"
cd C:\xampp\htdocs\site\lastaction\database\migrations && copy "2015_11_03_153725_create_clients_table.php" "2015_11_03_153725_create_d0clients_table.php"
cd C:\xampp\htdocs\site\lastaction\database\migrations && copy "2015_11_03_185333_create_transactions_table.php" "2015_11_03_185333_create_d0transactions_table.php"
cd C:\xampp\htdocs\site\lastaction\database\migrations && copy "2015_12_01_024822_create_jobs_table.php" "2015_12_01_024822_create_d0jobs_table.php"
cd C:\xampp\htdocs\site\lastaction\database\migrations && copy "2015_12_01_024854_create_logs_table.php" "2015_12_01_024854_create_d0logs_table.php"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0client.php" "Client" "D0client"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0transaction.php" "Transaction" "D0transaction"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0job.php" "Job" "D0job"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0log.php" "Log" "D0log"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0client.php" "clients" "d0clients"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0transaction.php" "transactions" "d0transactions"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0job.php" "jobs" "d0jobs"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\app\d0log.php" "logs" "d0logs"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_11_03_153725_create_d0clients_table.php" "clients" "d0clients"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_11_03_185333_create_d0transactions_table.php" "transactions" "d0transactions"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_12_01_024822_create_d0jobs_table.php" "jobs" "d0jobs"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_12_01_024854_create_d0logs_table.php" "logs" "d0logs"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_11_03_153725_create_d0clients_table.php" "CreateClientsTable" "CreateD0clientsTable"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_11_03_185333_create_d0transactions_table.php" "CreateTransactionsTable" "CreateD0transactionsTable"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_12_01_024822_create_d0jobs_table.php" "CreateJobsTable" "CreateD0jobsTable"
cd C:\xampp\htdocs\site && cscript replace.vbs "C:\xampp\htdocs\site\lastaction\database\migrations\2015_12_01_024854_create_d0logs_table.php" "CreateLogsTable" "CreateD0logsTable"
cd C:\xampp\htdocs\site\lastaction && composer dump

现在,当我添加一个新公司时,服务器会获取该company_id,然后修改复制基本迁移和模型的 .bat 文件以匹配新公司。之后,它启动 php artisan migrate。

现在,用户可以将数据添加到其公司的客户/交易表中,而无需与其他公司表交互。

也许我这样做是错误的......但它可以做我需要的事情。如果您有任何建议,请说/写下来!

关于php - Laravel 5.1 从 Controller 创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002703/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com