Laravel雄辩LEFT JOIN WHERE NULL [英] Laravel Eloquent LEFT JOIN WHERE NULL
问题描述
我尝试使用Eloquent在数据库种子中执行以下查询:
SELECT
*
从
客户
LEFT JOIN
订单
ON customers.id = orders.customer_id
WHERE
orders.customer_id IS NULL
这里是我在Enloft中的实现:
$ c = Customer :: leftJoin('orders',function($ join){
$ join-> on('customers.id','=','orders。 customer_id');
})
- > whereNull('orders.customer_id')
- > first();
而第一个查询始终返回完整的结果,Eloquent等效项总是返回空的元素,除了 Ex: 这是在dd()时输出的数组,对种子的第一次口述查询(最初由Faker生成): 可以通过从特定表格中指定所需的特定列名来解决,如下所示: I'm trying to use Eloquent to perform the following query during a database seed: And here is my implementation in Eloquent: Whereas the first query always returns full results, the Eloquent equivalent always returns empty elements for everything but the Ex: Here is the array that is output when I dd() the first Eloquent query on a seed (generated originally by Faker):
This can be resolved by specifying the specific column names desired from the specific table like so:
这篇关于Laravel雄辩LEFT JOIN WHERE NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!电子邮件
和电话
客户表中的字段。由于
客户
和订单
模型都是手工生成的骨架,我将无法解释这一点。 >
class Customer extends \Eloquent {
//在这里添加你的验证规则
public static $ rules = [
//'title'=> 'required'
];
//不要忘记填写这个数组
protected $ fillable = [];
}
protected $ original =>
数组(25){
'id'=>
NULL
'first_name'=>
NULL
'last_name'=>
NULL
'email'=>
string(24)luther.braun@example.org
'phone'=>
string(17)642.150.9176x5684
'address1'=>
NULL
'address2'=>
NULL
'city'=>
NULL
'state'=>
NULL
'county'=>
NULL
'district'=>
NULL
'postal_code'=>
NULL
'country'=>
NULL
'notes'=>
NULL
'created_at'=>
NULL
'updated_at'=>
NULL
'customer_id'=>
NULL
'total'=>
NULL
}
$ c = Customer :: leftJoin订单',功能($ join){
$ join-> on('customers.id','=','orders.customer_id');
})
- > whereNull('orders.customer_id')
- > first([
'customers.id',
'customers.first_name',
'customers.last_name',
'customers.email',
'customers.phone',
'customers.address1',
'customers.address2',
'customers.city',
'customers.state',
'customers.county',
'customers.district',
'customers.postal_code',
'customers.country'
]);
SELECT
*
FROM
customers
LEFT JOIN
orders
ON customers.id = orders.customer_id
WHERE
orders.customer_id IS NULL
$c = Customer::leftJoin('orders', function($join) {
$join->on('customers.id', '=', 'orders.customer_id');
})
->whereNull('orders.customer_id')
->first();
email
and phone
fields of the customers
table. I'm at a loss to explain this since the Customers
and Orders
models are both artisan generated skeletons.class Customer extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
];
// Don't forget to fill this array
protected $fillable = [];
}
protected $original =>
array(25) {
'id' =>
NULL
'first_name' =>
NULL
'last_name' =>
NULL
'email' =>
string(24) "luther.braun@example.org"
'phone' =>
string(17) "642.150.9176x5684"
'address1' =>
NULL
'address2' =>
NULL
'city' =>
NULL
'state' =>
NULL
'county' =>
NULL
'district' =>
NULL
'postal_code' =>
NULL
'country' =>
NULL
'notes' =>
NULL
'created_at' =>
NULL
'updated_at' =>
NULL
'customer_id' =>
NULL
'total' =>
NULL
}
$c = Customer::leftJoin('orders', function($join) {
$join->on('customers.id', '=', 'orders.customer_id');
})
->whereNull('orders.customer_id')
->first([
'customers.id',
'customers.first_name',
'customers.last_name',
'customers.email',
'customers.phone',
'customers.address1',
'customers.address2',
'customers.city',
'customers.state',
'customers.county',
'customers.district',
'customers.postal_code',
'customers.country'
]);