Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果没有',则拉威尔制作列;使用save()时不存在_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 如果没有',则拉威尔制作列;使用save()时不存在

Php 如果没有',则拉威尔制作列;使用save()时不存在,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我不知道这是否可能,但基本上我想要的是,当我保存()一个对象而没有找到一列时,它应该自动添加到表中 例如: $information = new App\Information; $meta = new App\Meta; //Insert real info for $information $information->save(); $meta->description = "random description" //This is an existing column $

我不知道这是否可能,但基本上我想要的是,当我保存()一个对象而没有找到一列时,它应该自动添加到表中

例如:

$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);
所以我想让它做的是在Meta表中创建列“newColumn”,因为它不存在

有没有一种方法可以在不创建新迁移的情况下做到这一点

编辑: 我想我找到了一种我认为是最好的方法

if (!Schema::hasColumn('meta', 'newColumn')){
  Schema::Table('meta',function($table){
    $table->string('newColumn');
  });
}
如果有人有更好的解决方案。请随意添加

最终编辑:


感谢大家的投入和帮助,但我们已经设计了一个新的数据库表,允许我们根据需要动态添加新数据。仍然感谢您的所有努力:)

我认为动态表格设计是个坏主意。为什么不创建一个新表,例如带有列的
字段

ID | Pid | FieldName | Content
其中ID是一个增量数字,Pid是保存在实际使用的
meta
表中的记录的ID

例子
Meta
表格:

ID | Description | StaticField1 | StaticField2
----------------------------------------------
1  | aaa         | a1           | a2
2  | bbb         | b1           | b2
3  | ccc         | c1           | c2
4  | ddd         | d1           | d2
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
字段
表格:

ID | Description | StaticField1 | StaticField2
----------------------------------------------
1  | aaa         | a1           | a2
2  | bbb         | b1           | b2
3  | ccc         | c1           | c2
4  | ddd         | d1           | d2
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
Meta.ID
相关,因此这意味着: Meta
aaa
有3个附加字段,分别命名为
字段1
字段2
字段3
。Meta
bbb
只有两个附加字段
字段1
字段2

因此,您现在只需在
fields
表中创建一条新记录,而不是在
Meta
表中为字段添加一个新列。

这是个坏主意。最好添加文本列
meta
,并在其中保存纯json。然后使用变数自动编码/解码。你的意思是将所有新内容保存为json在一列中?如果你有一个动态数据结构,最好将其保存为json或将一组
| foreign|id | key | value |
列保存在一个单独的表中。这实际上不是一个选项。每天都会有“新”数据,每天都会创建新的列。为了使报告系统正常工作,一切都需要齐心协力。因此,我的解决方案是使特定表中的几乎每一列都可以为null,因此并非所有对象都需要所有列。你怎么看?创建新列是没有选择的。不要这样做。如果我要为每个表的每个新列创建新表(有几个表将获得额外的列),我们的报告系统将遇到很多麻烦。还有其他建议吗?没有,您误解了,只有一个名为
fields
的表,其中包含原始表中所有记录的所有字段。您所谓的新列只是
字段
表中的一条新记录。我们计算出了它,并创建了一个全新的数据库模型。我们现在正在使用标签和标签类别,它们允许我们无限扩展,而不必添加可为空的collumn。无论如何,谢谢你的意见!