Как изменить оператор SQL на запрос laravel - PullRequest
0 голосов
/ 09 апреля 2020

У меня есть запрос sql, и я хочу изменить на laravel Запрос

Это sql

SELECT 
products.id, 
 products.name, 
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and DATE(stocks.created_at) 
= CURDATE()) as stock_in_today,

(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and 
  DATE(loadings.created_at) = CURDATE()) as total_loadings_today,

(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id) as total_stock_till_date,

 (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id) as 
total_loadings_till_date,

 ((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and 
 DATE(stocks.created_at) < CURDATE())-(select ifnull(sum(loadings.qty),0) from loadings where 
 loadings.pid=products.id and DATE(loadings.created_at) < CURDATE())) as opening_balance,

((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id)-(select 
ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id)) as closing_balance

  from products

кто-то может помочь в этом

1 Ответ

1 голос
/ 09 апреля 2020

Вы можете просто использовать DB фасад:

DB::select(DB::raw("SELECT 
products.id, 
 products.name, 
(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and DATE(stocks.created_at) 
= CURDATE()) as stock_in_today,

(select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id and 
  DATE(loadings.created_at) = CURDATE()) as total_loadings_today,

(select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id) as total_stock_till_date,

 (select ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id) as 
total_loadings_till_date,

 ((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id and 
 DATE(stocks.created_at) < CURDATE())-(select ifnull(sum(loadings.qty),0) from loadings where 
 loadings.pid=products.id and DATE(loadings.created_at) < CURDATE())) as opening_balance,

((select ifnull(sum(stocks.qty),0) from stocks where stocks.pid=products.id)-(select 
ifnull(sum(loadings.qty),0) from loadings where loadings.pid=products.id)) as closing_balance

  from products"));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...