modi.n
modi.n

Reputation: 41

How to apply 'ON CONFLICT' condition while Inserting records after SELECT statement in Laravel?

Below is the SQL query of which Laravel query is required.

    INSERT INTO 'details' (id, contact) 
    SELECT DISTINCT tmp_details.id, students.contact 
    FROM 'tmp_details'
    INNER JOIN 'students' USING(name) 
    ON CONFLICT DO NOTHING;    

I am not able to apply ON CONFLICT condition.

I have partially converted it as below.

    $strSQL =  DB::table('tmp_details')
                ->join('students', 'tmp_details.name', '=', 'students.name')
                ->select('tmp_details.id','students.contact');
               DB::table('details')->insertUsing(['id', 'contact'], $strSql);

Upvotes: 3

Views: 2287

Answers (2)

G.Baghashvili
G.Baghashvili

Reputation: 220

Try use DB::select() statement:

DB::select("
    INSERT INTO 'details' (id, contact) 
    SELECT DISTINCT tmp_details.id, students.contact 
    FROM 'tmp_details'
    INNER JOIN 'students' USING(name) 
    ON CONFLICT DO NOTHING;
");

Upvotes: 0

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Not sure about it but try this.

$strSQL =  DB::table('tmp_details')
              ->join('students', 'tmp_details.name', '=', 'students.name')
              ->pluck('tmp_details.id','students.contact');

DB::table('details')->insertOrIgnore($strSql);

Upvotes: 1

Related Questions