Search code examples
phplaravelmulti-tenant

Stancl/Tenancy - How do i automatically run the migrations when a new tenant is created?


When i create a new tenant the migrations for the new tenant doesnt run and I was wondering how I would automatically run the migrations for the newly created tenant after it is created. Is there an artisan command that has to be run in the background for it to work? Here is my controllers code so far when a request comes through to create a tenant.

public function store(Request $request)
    {
        $this->validate($request, [
            'company' => 'required',
            'domain' => 'required|unique:domains',
            'name' => 'required',
            'email' => 'required|unique:users,email',
            'password' => 'required|confirm'
        ]);
        $tenant_id = '-' . Str::slug($request->company, '-');
        $domain = $request->domain . '.' . 'saas.test';

        $tenant = Tenant::create([
            'id' => $tenant_id
        ]);


        $tenant->createDomain([
            'domain' => $domain
        ]);

        $tenant->run(function()
        {
            User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password)
            ]);
        });

        tenancy()->initialize($tenant);

        return redirect($domain);
    }

Solution

  • Try to run artisan command in your controller method: Artisan::call(‘tenants:migrate’);