雄辩:呼唤某个关系的地方 [英] Eloquent: Calling Where on a relation
本文介绍了雄辩:呼唤某个关系的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下雄辩的ORM查询。
$ products2 = Product :: with('metal','metal。
- > where('metal_id','=',1)
- > get() - > toArray();
此查询的输出如下:
我希望进一步缩小查询范围,只显示 fixes.currency_id = 1
的产品。
$ code $ $ 2 = Product :: with('metal','metal.fixes','metal.fixes.currency')
- >其中('metal_id','=' 1)
- > where('metal.fixes.currency_id','=',1)
- > get() - > toArray();
有人可以帮助我这个秒,因为我收到以下错误:
SQLSTATE [42S22]:未找到列:1054未知列'metal.fixes.currency_id'
在'where子句'(SQL :select * from`products`,`metal_id` =?
and`metal`.`fixes`.`currency_id` =?)(Bindings:array(0 => 1,1 => 1) )
在Rob Gordijn的帮助下解决:
$(
'metal',
'metal.fixes.currency',
'metal.fix '=> function($ query){
$ query-> where('currency_id','=',1);
}))
- > ','=',1)
- > where('metal_id','=',1)
- > get() - > toArray()
解决方案
您正在寻找Eager Load Constraints:< a href =http://laravel.com/docs/eloquent#querying-relations =nofollow> http://laravel.com/docs/eloquent#querying-relations
<?php
$ products2 = Product :: with(array('metal','metal.fixes','metal。 fixes.currency'=> function($ query){
$ query-> where('currency_id','=',1);
}))
- > where ('metal_id','=',1)
- > get() - > toArray();
I have the following Eloquent ORM query.
$products2 = Product::with('metal', 'metal.fixes', 'metal.fixes.currency')
->where('metal_id', '=', 1)
->get()->toArray();
Output from this query is as follows:
I wish to further narrow down my query to only display products where fixes.currency_id = 1
.
$products2 = Product::with('metal', 'metal.fixes', 'metal.fixes.currency')
->where('metal_id', '=', 1)
->where('metal.fixes.currency_id', '=', 1)
->get()->toArray();
Could somebody help me with this second where please because I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'metal.fixes.currency_id'
in 'where clause' (SQL: select * from `products` where `metal_id` = ?
and `metal`.`fixes`.`currency_id` = ?) (Bindings: array ( 0 => 1, 1 => 1, ))
Solved with the help of Rob Gordijn:
$products2 = Product::with(array(
'metal',
'metal.fixes.currency',
'metal.fixes' => function($query){
$query->where('currency_id', '=', 1);
}))
->where('common', '=', 1)
->where('metal_id', '=', 1)
->get()->toArray();
解决方案
You are looking for "Eager Load Constraints": http://laravel.com/docs/eloquent#querying-relations
<?php
$products2 = Product::with(array('metal', 'metal.fixes', 'metal.fixes.currency' => function($query){
$query->where('currency_id', '=', 1);
}))
->where('metal_id', '=', 1)
->get()->toArray();
这篇关于雄辩:呼唤某个关系的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文