lundi 30 novembre 2015

Laravel 5.1 Create table from controller

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire