13

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

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?


4
  • Try DB::table('users')->insert(array(1, 'Dayle')); insert uses an array for the values and you need to specify the table to do the insert on. Commented Dec 11, 2014 at 12:57
  • did ur database connect with the laravel and do u have users table inside that database ? Commented Dec 11, 2014 at 12:57
  • I was experimenting something with codeception the tool for testing. I found the mistake, i was using the wrong database, in video tutorial for codeception they don't use laravel. So problem is wrong database. Thank you for your reply. Commented Dec 11, 2014 at 13:00
  • I recommend to read the documentation: laravel.com/docs/4.2/queries#inserts Commented Dec 11, 2014 at 13:02

3 Answers 3

41
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?

Sign up to request clarification or add additional context in comments.

Comments

1
$user['id'] = 1;
$user['name'] = 'dale';

DB::table('users')->insert($user);

2 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
Please take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.
0

You can make simple like this

$val={
    'id_user' => '< ID_USER >',
    'title' => '< TITLE >',
    'body' => '< BODY >',
} 

Forum::create($val);

And its work

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.