0

I have a laravel eloquent query that is giving me a different result from the same query that I try at MariaDB

Eloquent:

$invoice = PurchaseInvoice::select("purchase_invoices.*", "purchase_contracts.contract_no", "partners.name as partner_name")
                    ->join("purchase_contracts","purchase_contracts.id","purchase_invoices.purchase_contract_id")
                    ->join("partners","partners.id","purchase_contracts.partner_id")
                    ->join("commodity_po","purchase_contracts.id","commodity_po.purchase_contract_id")
                    ->where("purchase_invoices.invoice_no", 'LIKE', '%'.$request->q.'%')
                    ->orWhere("purchase_invoices.po_additional_no", 'LIKE', '%'.$request->q.'%')
                    ->orWhere("purchase_contracts.contract_no", 'LIKE', '%'.$request->q.'%')
                    ->orWhereRaw("(CONCAT(`contract_no`, `po_additional_no`) LIKE '%?%')", [$request->q])->get()

and it will generate query like this :

select `purchase_invoices`.*, `purchase_contracts`.`contract_no`, `partners`.`name` as `partner_name` from `purchase_invoices` inner join `purchase_contracts` on `purchase_contracts`.`id` = `purchase_invoices`.`purchase_contract_id` inner join `partners` on `partners`.`id` = `purchase_contracts`.`partner_id` inner join `commodity_po` on `purchase_contracts`.`id` = `commodity_po`.`purchase_contract_id` where (purchase_invoices.invoice_no LIKE '%SLP0622/6A-%' or purchase_invoices.po_additional_no LIKE '%SLP0622/6A-%' or purchase_contracts.contract_no LIKE '%SLP0622/6A-%' or (CONCAT(`contract_no`, `po_additional_no`) LIKE '%SLP0622/6A-%')) and `purchase_invoices`.`deleted_at` is null;

when I run the query on laravel, it give me empty result

image for eloquent

but when I run the generated query on MySQL it give me 1 result

image for mysql

Does anyone know why this can happened? is this because there is a slash '/' on the query?

1
  • 1
    In orWhereRaw you are matching with exact value instead of contain. May be due to this you are geting null. * ->orWhereRaw("CONCAT(contract_no, po_additional_no)", 'like', '%'.$request->q.'%')->get()* Commented Jul 5, 2022 at 9:01

1 Answer 1

2

'%?%' is not a valid parameter. You need to pass it like:

->orWhereRaw("(CONCAT(`contract_no`, `po_additional_no`) LIKE ?)", [ '%'.$request->q.'%'])->get()
Sign up to request clarification or add additional context in comments.

1 Comment

whoa its working, thank you very much! I feel so stupid LOL

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.