Search code examples
phplaraveleloquentlaravel-artisan

Laravel - Create dynamically prefixed database tables based on separate table row creation


When I create a new "company" in the database, it gets an ID. Upon creation of that company, I want to dynamically create tables prefixed with this ID. For instance:

  1. Company 01 created in table Companies.
  2. Table 01_data_type created.
  3. Table 01_other_data_type created.

Step 1 is triggered by form. Steps 2-3 and beyond are triggered by step 1.

The best way I can think to do this inside Laravel is in my store method, upon store, call an artisan command for the migration and pass the ID. Is there a better way to do this?


Solution

  • The answer was provided by @Ohgodwhy in comments on question. spawn a job that creates the tables dynamically.