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

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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]);

1 Comment

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?

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.