Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shiro_'s avatar

Laravel Eloquent Query Passing Variables

I have a query that gives me a very large dataset of invoices. I am trying to get invoices only in a particular category using the where clause by passing a variable called lab_tests that contains all the category items . I keep getting this error SQLSTATE[HY093]: Invalid parameter number: parameter was not defined Is there a better way to pass the variable or filter out the categories?

My code

$lab_tests = LabTest::active()->orderBy('name', 'DESC')->get();

        $builder = InvoiceItem::select(
            DB::raw("date_closed"),
            DB::raw("invoices.number as invoice_number"),
            DB::raw("patients.first_name"),
            DB::raw("patients.last_name"),
            DB::raw("clinics.name"),
            DB::raw("invoice_items.name AS service_name"),
            DB::raw("invoice_items.total_amount"),
            DB::raw("invoice_items.amount_paid"),
            DB::raw("invoice_items.balance"),
            DB::raw("schemes.name as payment_mode")
            )
            ->join('invoices', 'invoices.id', 'invoice_items.invoice_id')
            ->join('payment_mode_visits', 'payment_mode_visits.visit_id', 'invoices.visit_id')
            ->join('schemes', 'schemes.id', 'payment_mode_visits.scheme_id')
            ->join('visits', 'visits.id', 'invoices.visit_id')
            ->join('patients', 'patients.id', 'visits.patient_id')
            ->join('clinics', 'clinics.id', 'visits.clinic_id')
            ->whereNotNull('date_closed')
            ->where('invoice_items.name', '=', $lab_tests);
            
        $invoices = $builder->get();

        return $invoices;
0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You are getting multiple

$lab_tests = LabTest::active()->orderBy('name', 'DESC')->pluck('name');

//and
->whereIn('invoice_items.name', $lab_tests);
1 like

Please or to participate in this conversation.