php Laravel 数据库插入查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27423289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:22:03  来源:igfitidea点击:

Laravel database insert query

phplaravel

提问by Vesko Vujovic

I'm trying to do simple insertion in database and im getting the following error

我正在尝试在数据库中进行简单的插入,但出现以下错误

call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

call_user_func_array() 期望参数 1 是一个有效的回调,没有给出数组或字符串

The insertion code look like this

插入代码如下所示

DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));

It's a basic query for laravel, but it won't work why?

这是 laravel 的基本查询,但为什么不起作用?



回答by Joe

DB::table('users')->insert(
     array(
            'id'     =>   '1', 
            'name'   =>   'Dayle'
     )
);

or

或者

$values = array('id' => 1,'name' => 'Dayle');
DB::table('users')->insert($values);

But why are you inserting an ID? Should this not be an auto incremented value?

但是你为什么要插入一个ID?这不应该是自动递增的值吗?