0

I'm trying to build in search functionality in my application (Laravel 5.1), but my join doesn't seem to do anything to the resulting query. What am I doing wrong?

Code:

$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);

$query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
      ->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));

$invoices = $query->paginate(15);

Resulting query:

select count(*) as aggregate from invoice_headers where customer_code_id = 1 and (invoice_types.name <> 380)

Resulting response:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invoice_types.name' in 'where clause'

This is the query I was hoping to see:

select count(*) as aggregate 
from invoice_headers 
inner join invoice_types 
   on invoice_headers.invoice_type_id = invoice_types.id
where customer_code_id = 1 
   and (invoice_types.name <> 380)

2 Answers 2

1
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);

you need to store the query in a variable.

$query = $query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
      ->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));

$invoices = $query->paginate(15);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add JOIN with invoice_types table if you want to filter on invoice_types.name.

15 Comments

Sorry, I pasted the wrong code. Please take a look again!
Looks like it's losing the join. Try to first do the join and then apply 2 where conditions.
I tried adding this: ->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES)); But it just added another "and invoice_types.name <> 380" to the query
Well, that's what you asked eloquent to do so it's no suprise. Have you tried first doing the join and then applying those 2 where constraints?
I tried separating the join and where into two statements before, if that's what you meant. Or did you mean something else?
|

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.