2017-01-13 57 views
0

我正在学习Laravel的过程中遇到了问题。laravel原始sql插入问题

我使用PHP Storm兼具MAMP Pro堆栈和远程LAMP堆栈。 我已将表迁移到两个数据库中都没有问题。 如果我然后运行下面的页面加载正确 - 没有错误抛出,但在MAMP Pro堆栈中,数据库仍然是空的。

Route::get('/insert', function(){ 
    DB::insert('insert into posts(title, body) values(?, ?)', ['PHP with Laravel', 'Laravel is the best thing to happen to PHP']); 
}); 

经过一些折腾了,我尝试了不同的策略 - 我手动插入数据到MAMP Pro数据库,工作正常。 我可以再使用DB::selectDB::update报表阅读和完全正常更新记录...

我不知所措,我为什么可以更新和读取表,而不是插入新记录?

回答

0

您应该利用DB::table()并从那里查询链接insert()方法。

这是正确的方法:

DB::table('posts')->insert([ 
    'title' => 'PHP with Laravel', 
    'body' => 'Laravel is the best thing to happen to PHP' 
]); 
+0

OK,林更糊涂了 - 当我运行的代码,它插入两个新行到表中,与出现在“标题”列的数据每一行。 –