使用 laravel query builder + mysql 从两个表中查询
query from two tables using laravel query builder + 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 | |
+--------------+------------------+------+-----+---------------------+-----------------------------+
您可以使用以下内容:
Quotation::has('invoices', '<', 1)->get();
以上代码假定您在 Quotation
模型中建立了关系,即:
class Quotation
{
public function invoices()
{
return $this->hasMany('\Models\Invoice');
}
}
has
方法将检查您定义为第一个参数 invoices
的关系中的项目总数。第二个参数是小于的比较,第三个是你要比较的计数。因此,这将搜索发票数量少于 1 的所有报价单。
您可以在查询关系存在下阅读更多内容about querying relations here。
感谢 Emn1ty
,我通过添加下面的代码让它工作
$quotations = Quotation::has('taxInvoices', '<', 1)->get();
我有两个表,一个叫报价单,另一个叫发票。我想检索所有没有发票的报价单。下面是我到目前为止的代码。我只能检索所有报价。如何修改此查询
$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 | |
+--------------+------------------+------+-----+---------------------+-----------------------------+
您可以使用以下内容:
Quotation::has('invoices', '<', 1)->get();
以上代码假定您在 Quotation
模型中建立了关系,即:
class Quotation
{
public function invoices()
{
return $this->hasMany('\Models\Invoice');
}
}
has
方法将检查您定义为第一个参数 invoices
的关系中的项目总数。第二个参数是小于的比较,第三个是你要比较的计数。因此,这将搜索发票数量少于 1 的所有报价单。
您可以在查询关系存在下阅读更多内容about querying relations here。
感谢 Emn1ty
,我通过添加下面的代码让它工作$quotations = Quotation::has('taxInvoices', '<', 1)->get();