0

我有两张表,一张称为报价单,另一张称为发票。我想检索所有没有发票的报价单。以下是我到目前为止的代码。我只能检索所有报价。如何修改此查询

$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 |                             |
+--------------+------------------+------+-----+---------------------+-----------------------------+
4

2 回答 2

0

您可以使用以下内容:

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

上面的代码假设您在Quotation模型中设置了关系,即:

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

has方法将检查您定义为第一个参数的关系中的项目总数invoices。第二个参数是小于的比较,第三个是要比较的计数。因此,这将搜索发票计数小于 1 的所有报价单。

您可以在查询关系存在下阅读更多关于查询关系的信息。

于 2016-01-31T01:23:01.730 回答
0

感谢 Emn1ty,我通过添加下面的代码让它工作了

$quotations = Quotation::has('taxInvoices', '<', 1)->get();
于 2016-02-01T02:09:52.120 回答