Laravel 5.1从控制器创建表

时间:2015-11-30 15:58:22

标签: php mysql laravel-5.1

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

我正在寻找在创建公司时创建客户表的正确方法。例如,当我创建公司A时,控制器在公司表中添加行A,然后检索company_id并创建clients_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

最后,我正在尝试为我的错误找到一个问题,我想知道是否有更容易的选项或任何其他建议以更好的方式执行此操作。

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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。

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

也许我这样做错了......但它可以做我需要的事情。如果你有任何建议,请说/写吧!