Laravel Query Builder连接不会影响查询

时间:2015-08-06 08:13:52

标签: php mysql laravel laravel-5 laravel-5.1

我正在尝试在我的应用程序(Laravel 5.1)中构建搜索功能,但我的连接似乎对生成的查询没有任何作用。我做错了什么?

代码:

$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);

结果查询:

  

invoice_headers customer_code_id = 1和invoice_types name&lt;&gt; 380)

选择计数(*)作为汇总

结果回复:

  

SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'invoice_types.name'

这是我希望看到的查询:

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 个答案:

答案 0 :(得分:1)

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

您需要将查询存储在变量中。

$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);

答案 1 :(得分:0)

如果您要在 invoice_types.name 上进行过滤,则需要在 invoice_types 表格中添加加入