0

I can't find the way connect multiple db and chain sql query,

How to use raw mysql syntax?

$id = DB::connection('mysql1')->select(DB::raw('INSERT INTO article_category (name) VALUES (:name)'), array(
    'name' => $name
));  // did I do something wrong this not work..

How to use laravel method?

$id = DB::connection('mysql1')->table('article_category')->insertGetId(
    array(
        'name' => $name
    )
);
user1775888
  • 3,147
  • 13
  • 45
  • 65

1 Answers1

3

You do raw this way:

DB::connection('mysql1')
    ->statement(
         DB::raw('insert into article_category (name) values (?)'),
         array($name)
    );

To get the Id after an insert this is a way:

Create a model for your table:

class Post extends Eloquent {}

And create a record on your table selecting a particular connection connection:

$post = Post::on('connectionName')->create($arrayOfvalues);

echo $post->id;
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • thanks for reply! I want to know both. – user1775888 May 09 '14 at 15:45
  • Edited to provide some more information on it. – Antonio Carlos Ribeiro May 09 '14 at 15:52
  • did I do something wrong with `DB::connection('mysql1')->select(DB::raw('INSERT INTO article_category (name) VALUES (:name)'), array( 'name' => $name ));` it is not work – user1775888 May 09 '14 at 16:08
  • Laravel uses positional `?` as query parameters so its `VALUES (?)` – Antonio Carlos Ribeiro May 09 '14 at 16:09
  • when I use this it will show error but it actually store success `DB::connection('mysql1')->select(DB::raw('INSERT INTO article_category (name) VALUES (:name)'), array('name' => $name ));` but when I change to `DB::connection('mysql1')->select(DB::raw('INSERT INTO article_category (name) VALUES (?)'), array('name' => $name ));` there's no error message but not store to database successful it is empty array – user1775888 May 09 '14 at 16:44
  • Odd, but try statement, edited to show how. – Antonio Carlos Ribeiro May 09 '14 at 16:50
  • if I use ? still not work store to database, but I change to :value it work and don't have error.. wondering what is statement() difference select()? – user1775888 May 09 '14 at 17:03