Laravel 4:如果通过查询关系存在关系,则选择行 [英] Laravel 4: Select row if a relation exists by querying relation
问题描述
迭代1 查询产品表,并希望它返回一个集合。 所有行,并且如果 $ name
匹配,则惰性加载金属表。这是错误的。
我的路线:
Route :: group (array('prefix'=>'{api} / v1'),function()
{
Route :: controller('products','Api\V1\ProductController');
});
我的控制器:
public function getFilter($ metal = null){
$ products = $ this-> product;
if($ metal){
$ products-> with('metal',function($ query,$ metal){
$ query-> where('name',$金属);
});
}
return Response :: api($ products-> get());
}
我只想 $ products
显示如果 metal.name = $ metal
。例如类似于:
$ this-> products-> where('metal.name',$ metal) - >得到;
使用Glad To Help的一部分解决方案的解决方案:
这提供了一种替代方法2,而不需要连接。
不幸的是,你不能用一个滑稽的敲门声来实现。
但是,有一种使用逆关系的方式,如下所示:
public function getFilter($ metal = null)
{
//过滤金属首先
$ metals =金属: :with('products') - > where('name','=',$ metal) - > get();
$ products = array();
foreach($ metals as $ metal)
{
//从过滤的金属中收集产品
$ products = array_merge($ products,$ metal-> products-> ; toArray());
}
return $ products;
}
如果这不是优雅的解决方案,您将不得不使用流利构造查询并手动加入产品x金属表,或通过覆盖newQuery()方法预先加入它们。
1)替代方法一。
public function getFilter($ metal = null){
return DB :: table('products') - > join('金属','products.id','=','metal.product_id')
- >其中('metal.name',$ name)
- > select(array *'));
}
2)替代方法二
class Product extends Eloquent {
public function newQuery($ excludeDeleted = true){
return parent :: newQuery() - >加入( '金属', 'ID', '=', 'metal.product_id');
}
}
I am trying to query a products table, and want it to return a collection if a relation exists.
Iteration 1 below queries all rows in the products table, and lazy loads the metals table if $name
matches. This is wrong.
My Route:
Route::group(array('prefix' => '{api}/v1'), function()
{
Route::controller('products', 'Api\V1\ProductController');
});
My Controller:
public function getFilter($metal = null) {
$products = $this->product;
if ($metal) {
$products->with('metal', function($query, $metal) {
$query->where('name', $metal);
});
}
return Response::api($products->get());
}
I want only $products
to display if metal.name = $metal
. e.g. something like:
$this->products->where('metal.name', $metal)->get;
Solution using part of Glad To Help's answer:
This provides an alternative approach 2, without the need for joins.
Unfortunately you cannot do this with one swipe in Eloquent yet.
BUT, there is a way by using the inverse relation, like this:
public function getFilter($metal = null)
{
// filter the metals first
$metals = Metal::with('products')->where('name', '=' , $metal)->get();
$products = array();
foreach($metals as $metal)
{
// collect the products from the filtered metals
$products = array_merge($products, $metal->products->toArray() );
}
return $products;
}
If this is not elegant solution for you, you will either have to use Fluent to construct the query and join the products x metals table manually or pre-join them by overriding the newQuery() method.
1) alternative approach one.
public function getFilter($metal = null) {
return DB::table('products')->join('metal', 'products.id', '=' , 'metal.product_id')
->where('metal.name', $name)
->select(array('products.*'));
}
2) alternative approach two
class Product extends Eloquent{
public function newQuery($excludeDeleted = true){
return parent::newQuery()->join('metal','id','=','metal.product_id');
}
}
这篇关于Laravel 4:如果通过查询关系存在关系,则选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!