0

I have a query but i don't know how to run in Laravel

WITH RECURSIVE category_path (id, name, lvl) AS(SELECT id, name, 0 lvl FROM category WHERE parent =1  UNION ALL SELECT c.id, c.name,cp.lvl + 1 FROM category_path AS cp JOIN category AS c ON cp.id = c.parent) SELECT * FROM category_path GROUP BY lvl ORDER BY lvl DESC limit 1

2 Answers 2

2

You can use DB::select('your query here'); here is official link to doc laravel

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

Comments

0

I Think You want to use Raw Query You Can try this:

DB::select(DB::raw('your query here'));

5 Comments

Can you show me what You get in response by this method
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RECURSIVE category_path (id, title, lvl) AS ( SELECT id, title, 0 lvl FROM' at line 1 (SQL: WITH RECURSIVE category_path (id, title, lvl) AS ( SELECT id, title, 0 lvl FROM category WHERE parent_id IS NULL UNION ALL SELECT c.id, c.title,cp.lvl + 1 FROM category_path AS cp JOIN category AS c ON cp.id = c.parent_id ) SELECT * FROM category_path ORDER BY lvl)
Does your query run if executed directly in sql?
This is a syntax error in your query laravel is executing query perfectly try to run your query in phpmyadmin
mysql version mismatch

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.