Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel database insert query

Tags:

php

laravel

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?


like image 417
Vesko Vujovic Avatar asked Dec 11 '14 12:12

Vesko Vujovic


People also ask

What is query () in Laravel?

In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.

How do you put data in a relationship in Laravel?

Code: $user = new User; $inputArry = array('data1' => $request['field1'], 'data2' => $request['field2'], 'data3' => $request['field3'], ); $user->create($inputArry); $user->xyz()->create([ 'user_id' => $user->id, 'name' => $request['name'], 'about' => $request['desc'], 'tag' => $request['tag'], ]);

How can we get data from database in controller in Laravel?

After configuring the database, we can retrieve the records using the DB facade with select method. The syntax of select method is as shown in the following table. Run a select statement against the database.


2 Answers

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

DB::table('users')->insert($user);
like image 137
fahad sulaiman Avatar answered Oct 10 '22 03:10

fahad sulaiman


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?

like image 42
Joe Avatar answered Oct 10 '22 03:10

Joe