0

I don't know if it's possible but basically what I want is when i save() an object and a column is not found, it should be automatically added to the table.

For example:

$information = new App\Information;
$meta = new App\Meta;

//Insert real info for $information
$information->save();

$meta->description = "random description" //This is an existing column
$meta->newColumn = "Random string" //This column doesn't exist

$information->meta()->save($meta);

So what I want it do is create the column "newColumn" in the table Meta because it doesn't exist.

Is there a way of doing this without creating a new migration?

EDIT: I have found a method which I think is the best one to do it, I guess..

if (!Schema::hasColumn('meta', 'newColumn')){
  Schema::Table('meta',function($table){
    $table->string('newColumn');
  });
}

If someone has a better solution. Feel free to add

FINAL EDIT:

Thanks everyone for your input and help but we have designed a new database table that allows us to dynamically add new data as we please. Still thanks for all of your efforts :)

VJamie
  • 616
  • 3
  • 14
  • It's a bad idea. Better add TEXT column `meta` and save plain json in it. Then use mutators to automatically encode/decode it. – Maxim Lanin Jul 31 '15 at 09:31
  • You mean saving all new stuff as json in 1 column? – VJamie Jul 31 '15 at 09:33
  • 1
    If you have a dynamic data structure it is always better to save it as json or a set of `| foreign_id | key | value | ` columns in a separate table. – Maxim Lanin Jul 31 '15 at 09:38
  • Well that's not really an option here. There will be "new" data every day and new Columns will be made every day. Everything needs to be together for the reporting system to work. So my solution was to make pretty much every column in the specific table Nullable so not all objects have to require all the columns. What do you think about it? – VJamie Jul 31 '15 at 09:41
  • http://stackoverflow.com/questions/695752/how-to-design-a-product-table-for-many-kinds-of-product-where-each-product-has-m#695860 Not related to laravel, but you'll find answers to you database design question. – Needpoule Jul 31 '15 at 09:47
  • 1
    Creating new columns is no option. Don't do that. – Pᴇʜ Jul 31 '15 at 09:48
  • Creating new columns every day could result in massive database issues over time. – Mhluzi Bhaka Jul 31 '15 at 10:06
  • What database are you using? Seems like a good project for schema-less databases like MongoDB – vfsoraki Jul 31 '15 at 11:58
  • Read final edit, thanks though for all your valid input :) – VJamie Jul 31 '15 at 12:10

1 Answers1

2

I think a dynamic table design is a bad idea. Why don't you create one new table e.g. fields with the columns

ID | Pid | FieldName | Content

where ID is an incremental number and Pid is the ID of a record saved in the meta table you are actually using.

Example

Meta table:

ID | Description | StaticField1 | StaticField2
----------------------------------------------
1  | aaa         | a1           | a2
2  | bbb         | b1           | b2
3  | ccc         | c1           | c2
4  | ddd         | d1           | d2

Fields table:

ID | Pid | FieldName | Content
------------------------------
1  | 1   | field 1   | content 1
2  | 1   | field 2   | content 2
3  | 1   | field 3   | content 3
4  | 2   | field 1   | content 4
5  | 2   | field 2   | content 5

Fields.Pid is related to Meta.ID so this means: Meta aaa has 3 additional fields named field 1, field 2 and field 3. Meta bbb has only 2 additional fields field 1 and field 2. So instead of adding a new column for a field into the Meta table you now just create a new record into the fieldstable.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • If i'm going to create new tables for every new column of every table (there are several tables who are going to get additional columns) our reporting system will get in a lot of trouble. Any other suggestions? – VJamie Jul 31 '15 at 09:43
  • no you misunderstood there is only one table called `fields` which contains all fields of all records of the original table. What you called a new column is just a new record in the `fields` table. – Pᴇʜ Jul 31 '15 at 09:45
  • We figured it out and made a whole new database model. we're now working with tags and tag categories which allows us to expand infinitely without having to add nullable collumns. Anyway thanks for your input! – VJamie Jul 31 '15 at 10:19