0

Laravel Query Builder

     $data = CustomerPrepaid
            ::join('pos_sales', 'customer_prepaid.customer_id', '=', 'pos_sales.customer_id')
            ->join('pos_sales_product', 'pos_sales.pos_sales_code', '=', 'pos_sales_product.pos_sales_code')
            ->where('pos_sales_product.product_id', 'customer_prepaid.product_id')
            ->select('customer_prepaid.customer_id', 'customer_prepaid.created_at',
            'pos_sales_product.pos_sales_product_code as reference_no', 

'customer_prepaid.product_id', 'customer_prepaid.balance', 
        'last_used', 'expiry_date', 'customer_prepaid.amount as price')
        ->offset(($page-1)*$limit)->limit($limit)->get();

SQL

SELECT customer_prepaid.customer_id, customer_prepaid.created_at as purchase_date,
pos_sales_product.pos_sales_product_code as reference_no, customer_prepaid.product_id, 
customer_prepaid.balance, customer_prepaid.amount*customer_prepaid.balance as value, 
last_used, expiry_date, customer_prepaid.amount as price,
customer_prepaid.amount*customer_prepaid.balance as total
FROM customer_prepaid
JOIN pos_sales ON customer_prepaid.customer_id = pos_sales.customer_id
JOIN pos_sales_product ON pos_sales.pos_sales_code = pos_sales_product.pos_sales_code
WHERE pos_sales_product.product_id = customer_prepaid.product_id 

The resulting SQL executed on the server returns the right result, but I get no eloquent result, why might that be?

4
  • Did you check the SQL generated by the Query Builder using ->toSql() instead of ->offset(($pag..... ? Commented Jan 22, 2018 at 8:34
  • After checked with toSql(),I get this result. puu.sh/z70cw/4a4cddc975.png Commented Jan 22, 2018 at 8:40
  • 2
    Could it be $page is giving you paged result? Have you tried the query builder without offset and limit? Commented Jan 22, 2018 at 8:55
  • I test already remove offset(), but get no result. Commented Jan 22, 2018 at 9:20

1 Answer 1

1

Oh gosh, took me forever to realize you misused ->where.

Change your ->where to ->whereColumn:

$data = CustomerPrepaid
        ::join('pos_sales', 'customer_prepaid.customer_id', '=', 'pos_sales.customer_id')
        ->join('pos_sales_product', 'pos_sales.pos_sales_code', '=', 'pos_sales_product.pos_sales_code')
        ->whereColumn('pos_sales_product.product_id', 'customer_prepaid.product_id')
        ->select(
            'customer_prepaid.customer_id',
            'customer_prepaid.created_at',
            'pos_sales_product.pos_sales_product_code as reference_no',
            'customer_prepaid.product_id', 'customer_prepaid.balance',
            'last_used', 'expiry_date', 'customer_prepaid.amount as price'
        )
        ->offset(($page-1)*$limit)
        ->limit($limit)
        ->get();

You have to use whereColumn instead of where to make column comparison. Else it's expecting a third parameter value to be set.

Check the documentation on how to use whereColumn: https://laravel.com/docs/5.5/queries#where-clauses

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

1 Comment

Thanks you sir. like that ok :)

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.