2016-01-31 128 views
0

我有兩個表,一個叫做引號,另一個叫發票。我想檢索所有沒有發票的報價單。以下是我到目前爲止的代碼。我只能檢索所有報價。我怎麼能修改此查詢查詢從兩個表使用laravel查詢生成器+ mysql

$quotations = Quotation::lists('id', 'id'); 


mysql> describe quotations; 
+---  ---- --------+------------------+------+-----+---------------------+-----------------------------+ 
| Field   | Type    | Null | Key | Default    | Extra      | 
+---------------+------------------+------+-----+---------------------+-----------------------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment    | 
| customer_id | int(10) unsigned | NO | MUL | NULL    |        | 
| employee_id | int(10) unsigned | NO | MUL | NULL    |        | 
| exchange_rate | int(11)   | NO |  | 0     |        | 
| remark  | varchar(255)  | NO |  |      |        | 
| created_at | timestamp  | NO |  | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |        | 
+---------------+------------------+------+-----+---------------------+-----------------------------+ 
mysql> describe invoices; 
+--------------+------------------+------+-----+---------------------+-----------------------------+ 
| Field  | Type    | Null | Key | Default    | Extra      | 
+--------------+------------------+------+-----+---------------------+-----------------------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment    | 
| quotation_id | int(10) unsigned | NO | MUL | NULL    |        | 
| employee_id | int(10) unsigned | NO | MUL | NULL    |        | 
| amount  | int(11)   | NO |  | 0     |        | 
| balance  | int(11)   | NO |  | 0     |        | 
| created_at | timestamp  | NO |  | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |        | 
+--------------+------------------+------+-----+---------------------+-----------------------------+ 

回答

0

您可以使用以下方法:

Quotation::has('invoices', '<', 1)->get();

這上面的代碼假設你有你的Quotation模型建立的關係,即:

class Quotation 
{ 
    public function invoices() 
    { 
     return $this->hasMany('\Models\Invoice'); 
    } 
} 

has方法將檢查您已定義爲第一個參數invoices的關係中的項目總數。第二個參數是小於的比較,第三個是你想比較的數量。因此,這將搜索發票數量小於1的所有報價。

您可以閱讀更多about querying relations here根據查詢關係存在

+0

感謝您的快速響應工作。請澄清你的答案。 '發票'不是一個屬性。我想獲取發票表 –

+0

中沒有條目的所有報價啊,我誤讀了關係。我會更新我的答案。 – jardis

+0

感謝你的回答,你的回答是正確的,但你需要添加 - > get();爲它工作。它應該像Quotation :: has('invoices','<', 1)-> get(); –

0

我把它加入感謝下面的代碼Emn1ty

$quotations = Quotation::has('taxInvoices', '<', 1)->get();