Laravel Eloquent 主从结果
Laravel Eloquent Master Detail result
我正在使用 Laravel 5.1
只是想问一下当子 return 没有主数据未显示
时如何显示 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' 区域内过滤代码时不显示主记录,使用 ->has('telcoProduct) 仍然对我不起作用。主记录仍然显示在 Blade Views 上。
有什么技巧可以解决这个问题吗?
在 with
之前添加一个 whereHas
函数。这本质上就像说 "if you have any, give me all records matching these criteria".
您的查询应如下所示(未测试)。
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;
}
谢谢@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->whereMemType(1);
显示telcoProduct join里面的所有记录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"
}
]
我的技术正确吗?我应该在查询中写两次条件吗?
感谢
我正在使用 Laravel 5.1 只是想问一下当子 return 没有主数据未显示
时如何显示 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' 区域内过滤代码时不显示主记录,使用 ->has('telcoProduct) 仍然对我不起作用。主记录仍然显示在 Blade Views 上。 有什么技巧可以解决这个问题吗?
在 with
之前添加一个 whereHas
函数。这本质上就像说 "if you have any, give me all records matching these criteria".
您的查询应如下所示(未测试)。
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;
}
谢谢@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->whereMemType(1);
显示telcoProduct join里面的所有记录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"
}
]
我的技术正确吗?我应该在查询中写两次条件吗?
感谢