0

please help me.

I've got the raw SQL

WITH collections AS (
    SELECT 
        ...
    FROM
        ...
)
SELECT
    ...
FROM collections c1
LEFT JOIN collections c2
    ...

How I can write it using Laravel QueryBuilder?

1
  • 1
    Laravel QueryBuilder does not support WITH syntax. You have 2 options: rewrite query without 'WITH' or use DB::select() which accepts raw sql queries. Commented Nov 15, 2016 at 6:07

1 Answer 1

1

You may use the RAW method of Laravel's Query Builder (https://laravel.com/docs/5.3/queries#raw-expressions). Just be careful with potential SQL injection loopholes.

Sign up to request clarification or add additional context in comments.

5 Comments

Now I'm doing this: $queryResult = DB::select(" with collections AS ( SELECT .....))";
Can you post the entire query statement that you made?
$queryResult = DB::select(" with collections AS ( SELECT tcm.shop_id, ... FROM template_collection_menu AS tcm LEFT JOIN template_collection AS tc ON tc.id = tcm.template_collection_id WHERE tcm.shop_id IN (:mainShopId, :currentShopId) ORDER BY tcm.position ASC ) SELECT COALESCE(c2.shop_id, c1.shop_id) as shop_id, ... FROM collections c1 LEFT JOIN collections c2 ON c2.template_collection_id = c1.template_collection_id AND c1.shop_id = :mainShopId AND c2.shop_id = :currentShopId WHERE c1.shop_id = :mainShopId ", ['mainShopId' => $mainShopId,...]);
Have you checked the URL that I gave? You have to use DB::raw instead of DB::select.
@IlyaPoluhin, you can use the DB::raw like this DB::raw('SELECT * FROM table...)

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.