How can I make this query in Laravel , im new in laravel
Product_Type::addSelect([
'p_name' => Product::select('p_name')->whereColumn('products.id','product_types.products_id'),
'product_types.pt_name',
'sale_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
'return_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','return')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
])->where('product_types.status',1)->get()
its working but i want to get only sale_qty > 0 so when set sale_qty query in where clause then getting error i want to correct this query
Product_Type::addSelect([
'p_name' => Product::select('p_name')->whereColumn('products.id','product_types.products_id'),
'product_types.pt_name',
'sale_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
'return_qty' => Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','return')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))),
])->where('product_types.status',1)
->whereRaw(Stock::selectRaw('SUM(stock_value)*-1')
->whereColumn('stocks.product_types_id','product_types.id')
->whereRaw('stock_value < 0')
->where('stock_type','sale')
->whereBetween('stock_date',[$reqFromDate,$reqToDate])
->whereIn('id', Bill::select('stocks_id')
->where('status',1)
->whereIn('customers_id', Customer::select('id')
->where('customers.sale_mans_id',$reqSalemanId))) > 0)->get()
in MySQL query which show correct result as per my requirements
SELECT
(SELECT p_name FROM products WHERE id=product_types.products_id) AS p_name,
pt_name,
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='sale' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) AS sale_qty,
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='return' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) AS return_qty
FROM product_types
WHERE
status =1 AND
(SELECT SUM(stock_value)*-1 FROM stocks WHERE product_types_id=product_types.id AND stock_value < 0 AND stock_type='sale' AND stock_date BETWEEN '2022-07-01' AND '2022-08-31' AND id IN (SELECT stocks_id FROM bills WHERE status=1 AND customers_id IN (SELECT id FROM customers WHERE sale_mans_id=1))) > 0;
because where clause not allow without column name so how manage without column name
You can use ->whereRaw
and define the condition as you like. It will be inserted as a string to the full query without any modifications. This way you have total control over it.
Check the related documentation: https://laravel.com/docs/9.x/queries#whereraw-orwhereraw
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.