M
M
Mikhail Smirnov2021-12-25 14:11:37
Laravel
Mikhail Smirnov, 2021-12-25 14:11:37

How to create a SQL query in Laravel in Query Builder?

SELECT
  f.facet_id,
  f.value,
  MIN(f.value_num) min_value_num,
  MAX(f.value_num) max_value_num,
  COUNT(DISTINCT f.product_id) AS cnt
FROM catalog_index f

INNER JOIN (

  SELECT
    cp.id
  FROM catalog_products cp
  INNER JOIN catalog_index cf ON cf.product_id = cp.id
  WHERE 1 = 1
    AND cp.section_id = 440
    AND cf.facet_id = 7718
    AND cf.value IN (285)

) p ON p.id = f.product_id

WHERE f.section_id = 440
GROUP BY f.facet_id, f.value


How to create a SQL query in Laravel in Query Builder? Interested in innerJoin

Or somehow you can just stick in just sql without a constructor
Before laravel worked with Yii2, there is a createCommand for pure SQL

$connection = Yii::$app->getDb();
$command = $connection->createCommand('SELECT * FROM users');

$result = $command->queryAll();


I didn’t find anything like this in Laravel, most likely I didn’t look well, but I didn’t find it)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Smirnov, 2021-12-25
@fortoster82

Found everything)
DB::select('')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question