This is the situation: I have a list of user, this user are associate to a company and those companies are associate to a group. Each user can create a client who is associate to the user and the company in which the user belong. For the moment, all my client are in the same database table. But I want to have a clients table for each company.
I'm looking for the right way to create a clients table when i create a company. Example, when I create company A, the controller add the row A in the companies table then retrieve the company_id and create clients_id table. For now, I tried this:
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');
}
I added this :
use Illuminate\Database\Schema\Blueprint;
But I got this error :
Class 'App\Http\Controllers\Admin\Schema' not found
Finally, I am trying to find an issue for my error and I want to know if there is an option to make it easier or any other suggestion to do it in a better way.
Thank you!
Some one put this and deleted his comment:
use Illuminate\Support\Facades\Schema as Schema;
@Tim Lewis said it too. That solved my problem. But when I tested it, I faced another problem. I was not creating a model, so I was unable to add row using
$client = new Client;
This is what I've done:
Firstly, I change the function storeCompany to this:
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');
}
Secondly, I created this .bat file:
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
So now, when I add a new Company, the server get this company_id then modify the .bat file who copy base migration and model to match the new company. After this it launch the php artisan migrate.
Now, users can add data to the client/transaction table of their company without interacting with other companies tables.
Maybe I'm wrong doing this... But it work to do what I need. If you have any suggest, say/write it!