0

Sometime, I can make can use Eloquent for complex login in laravel. So, i need run a big raw query. How to do that? Example: i need run

Insert Into ..............
       Select..............
       Join....
       Join....
       Where...............
       ...............................................................
  • 2
    https://laravel.com/docs/master/queries#raw-expressions – xNoJustice Jul 31 '20 at 08:38
  • Does this answer your question? [How to execute raw queries with Laravel 5.1?](https://stackoverflow.com/questions/33049511/how-to-execute-raw-queries-with-laravel-5-1) – Egretos Jul 31 '20 at 11:42

3 Answers3

4

May be you're finding for this solution. you can using it for resolve your problem.

$yourComplexQuery = "
    Insert Into ..............
    Select..............
    Join....
    Join....
    Where...............
   ...............................................................
";
DB::select($yourComplexQuery);
Thanh Nguyen
  • 390
  • 4
  • 6
1

You can use the DB Facade, in addition you could use its raw methods.

DB::table('tableName')->selectRaw()...

https://laravel.com/docs/master/queries#raw-expressions

If this is not raw enough for you, you can simply define your sql query:

$query = "Insert Into ..............
       Select..............
       Join....
       Join....
       Where...............
       ................... ";

and insert it into the DB::select($query);

Aless55
  • 2,652
  • 2
  • 15
  • 26
0
DB::insert("
    insert into table1 (column1, column2, column3) 
    select columnA, columnB, columnC 
    from table2 
    join table3 on table2.id = table3.table2_id 
    join table4 on table3.id = table4.table3_id 
    where table4.columnX = ?", [$value]);
Anil Tomar
  • 82
  • 4
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 26 '23 at 01:14