Php Laravel雄辩的大师级细节成果
我使用的是Laravel 5.1 只想问一下,当子级未返回主数据未显示的数据时,如何显示ORM主数据详细信息 这是我的主模型Php Laravel雄辩的大师级细节成果,php,laravel,eloquent,relationship,laravel-5.1,Php,Laravel,Eloquent,Relationship,Laravel 5.1,我使用的是Laravel 5.1 只想问一下,当子级未返回主数据未显示的数据时,如何显示ORM主数据详细信息 这是我的主模型 <?php namespace SpekWeb; use Illuminate\Database\Eloquent\Model; class ProdukGroup extends Model { public $table = 'product_group'; public $primaryKey = 'prd
<?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;
}
}
当我使用->has'telcoProduct过滤“with”区域内的代码时,我希望主记录不显示,但它仍然不适用于我。主记录仍显示在刀片视图上。
有什么技巧可以解决这个问题吗?在使用之前添加whereHas函数。这基本上就像是说,如果你有,给我所有符合这些条件的记录
您的查询应该是这样的:未测试
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;
}
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
},
]
请检查电信产品的第二条记录,它与查询条件不匹配
$tpHas->whereMemType1
它显示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"
}
]
我的技术正确吗?我应该在查询上写两次条件吗
Thx修改后的查询只返回ProdukGroup数据。我希望$memberPrice也返回其子记录。请参阅我更新的帖子。whereHas只检查特定条件,但不立即加载它们。是的,不幸的是,这是正确的。这些条件不共享,因此您必须使用whereHas筛选出所有父对象,然后使用加载它们。谢谢你指出我代码中的缺陷,我会更新的。