我正在使用Laravel 5.1 只想询问如何在子数据没有显示主数据未显示的数据时显示ORM主数据
这是我的主模型
<?php
namespace SpekWeb;
use Illuminate\Database\Eloquent\Model;
class ProdukGroup extends Model
{
public $table = 'product_group';
public $primaryKey = 'prd_group';
public function telcoProduct() {
return $this->hasMany('SpekWeb\TelcoProduct', 'prd_group', 'prd_group');
}
}
儿童模特
<?php
namespace SpekWeb;
use Illuminate\Database\Eloquent\Model;
class TelcoProduct extends Model
{
public $table = 'telco_product';
public $primaryKey = 'tel_prd';
}
这是我的控制器
<?php
namespace SpekWeb\Http\Controllers;
use Illuminate\Http\Request;
use SpekWeb\Http\Requests;
use SpekWeb\Http\Controllers\Controller;
use SpekWeb\ProdukGroup;
use SpekWeb\TelcoProduct;
class ProdukController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$memberPrices = ProdukGroup::with(array(
'telcoProduct' => function($tpJoin) {
$tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$tpJoin->where('charge','>=',0);
$tpJoin->whereMemType('1'); //variabel
$tpJoin->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //variabel
$qWhere->orWhereNull('cluster_gid');
});
},
)
)
->has('telcoProduct')
->get();
return $memberPrices;
}
}
我希望当我在'with'区域内过滤代码时没有显示主记录,使用 - &gt; has('telcoProduct)仍然不适合我。主记录仍显示在Blade Views上。 有没有办法解决这个问题?
答案 0 :(得分:0)
在whereHas
之前添加with
功能。这基本上就像是说“如果你有,请给我所有符合这些标准的记录”。
您的查询应如下所示(未经过测试)。
public function index()
{
$memberPrices = ProdukGroup::whereHas('telco_product', function($query) {
$query->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$query->where('charge','>=',0);
$query->whereMemType('1'); //variabel
$query->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //variabel
$qWhere->orWhereNull('cluster_gid');
});
},
)
)
->with(array(
'telcoProduct' => function($tpJoin) {
$tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$tpJoin->where('charge','>=',0);
$tpJoin->whereMemType(1); //this value replace by dynamic Input
$tpJoin->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
$qWhere->orWhereNull('cluster_gid');
});
},
))
->get();
return $memberPrices;
}
答案 1 :(得分:0)
Thanx @Tim
但是当我按照你的意思使用这段代码时:
$memberPrices = ProdukGroup::whereHas('telcoProduct', function($tpHas){
$tpHas->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$tpHas->where('charge','>=',0);
$tpHas->whereMemType(1); //this value replace by dynamic Input
$tpHas->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
$qWhere->orWhereNull('cluster_gid');
});
}
)
->with(array(
'telcoProduct' => function($tpJoin) {
$tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
},
))
->has('telcoProduct')
->get();
return $memberPrices;
我得到了这个结果:
"prd_group": 1,
"grp_name": "Telkomsel",
"topup_code": "777",
"balance_code": "776",
"tel_id": 1,
"product_type": "reguler",
"with_code": 1,
"telco_product": [
{
"tel_prd": 4,
"prd_group": 1,
"prd_value": 5000,
"stock": null,
"keyword": "S5",
"charge": 0,
"price": 5000,
"mem_type": 1,
"prd_id": 4,
"cluster_gid": null,
"kode_kec": null
},
{
"tel_prd": 4,
"prd_group": 1,
"prd_value": 5000,
"stock": null,
"keyword": "S5",
"charge": 0,
"price": 5100,
"mem_type": 2,
"prd_id": 4,
"cluster_gid": null,
"kode_kec": null
},
]
请检查telco_product的第二条记录,它与查询条件
不符$ tpHas-&GT; whereMemType(1);
显示telcoProduct内的所有记录加入wd_productprice
我已经尝试了这段代码:
$memberPrices = ProdukGroup::whereHas('telcoProduct', function($tpHas){
$tpHas->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$tpHas->where('charge','>=',0);
$tpHas->whereMemType(1); //this value replace by dynamic Input
$tpHas->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
$qWhere->orWhereNull('cluster_gid');
});
}
)
->with(array(
'telcoProduct' => function($tpJoin) {
$tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
$tpJoin->where('charge','>=',0);
$tpJoin->whereMemType(1); //this value replace by dynamic Input
$tpJoin->where(function($qWhere) {
$qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
$qWhere->orWhereNull('cluster_gid');
});
},
))
->has('telcoProduct')
->get();
return $memberPrices;
我得到了预期的结果
"prd_group": 1,
"grp_name": "Telkomsel",
"topup_code": "777",
"balance_code": "776",
"tel_id": 1,
"product_type": "reguler",
"with_code": 1,
"telco_product": [
{
"tel_prd": 4,
"prd_group": 1,
"prd_value": 5000,
"stock": null,
"keyword": "S5",
"charge": 0,
"price": 5000,
"mem_type": 1,
"prd_id": 4,
"cluster_gid": null,
"kode_kec": null
},
{
"tel_prd": 4,
"prd_group": 1,
"prd_value": 5000,
"stock": null,
"keyword": "SS5",
"charge": 0,
"price": 5000,
"mem_type": 1,
"prd_id": 148,
"cluster_gid": 3,
"kode_kec": "ASTAY"
}
]
我的技术是否正确?我应该在查询中写两次条件吗?
THX