如何建立网站会员系统吗,中企动力电话,网站怎么做备份,淡水做网站一#xff0c;多表关联
文章表articles 和user_id,category_id关联
//with()方法是渴求式加载#xff0c;缓解了1N的查询问题#xff0c;仅需11次查询就能解决问题#xff0c;可以提升查询速度。with部分没有就以null输出#xff0c;所以可以理解为 多表 left join 查…一多表关联
文章表articles 和user_id,category_id关联
//with()方法是渴求式加载缓解了1N的查询问题仅需11次查询就能解决问题可以提升查询速度。with部分没有就以null输出所以可以理解为 多表 left join 查询
//has()和whereHas() 是基于关联关系去过滤模型的查询结果,如果关联模型不符合条件那整体就没有数据。所以可以理解为多表 inner join 查询。模型关系
public function user(){return $this-belongsTo(App\Models\User,user_id,id);
}//表2public function category(){return $this-belongsTo(App\Models\Category,category_id,id);}控制器
//输出文章表中所有文章数据如果符合用户id1,分类name科技 就输出关联的信息没有就是null类似left join 查询
$list Article::with([userfunction($query){$query-where(id, , 1);},categoryfunction($query){$query-where(name, , 科技);},模型定义的方法名function($query){$query-where(name, , 科技);}
])
-where(status,1)
-get();//输出文章表中符合条件的文章需要满足用户id1,分类name科技 的条件类似 inner join
//这里要注意whereHas和has只做筛选并不返回关系user和category中的数据但是 with 是返回的。所以当要返回关系数据时两者要结合使用。
$list Article::whereHas(user,function($query){$query-where(id, , 1);
})
-whereHas(category,function($query){$query-where(name, , 科技);
})
//这里的with 其实不用再写条件也行因为前面的whereHas 已经限定了条件了
-with([userfunction($query){$query-where(id, , 1);},categoryfunction($query){$query-where(name, , 科技);}
])
-where(status,1)
-get();实际使用
//$request-has() 判断参数是否存在$request-filled() 判断参数存在且不为空
$id $request-get(id);
$list Article::whereHas(user,function($query) use ($id){if(!empty($id)){$query-where(id, , $id);}})
-whereHas(category,function($query){$query-where(name, , 科技);
})
//这里的with 其实不用再写条件也行因为前面的whereHas 已经限定了条件了
-with([userfunction($query){$query-where(id, , 1);},categoryfunction($query){$query-where(name, , 科技);}
])
-where(status,1)
-get();二多表多层级嵌套关联查询
一个company有多个store,一个store有多个goods
$limit $request-limit ?? 10;
$query Goods::query()-where([status 1]);
// 如果存在请求的公司获取请求公司所有商家的每款产品
if ($request-company_ids){$companyIdsArr explode(,, $request-company_ids);$query-whereHas(business, function ($query) use($companyIdsArr){$query-whereHas(commpany, function ($query) use($companyIdsArr){$query-whereIn(id, $companyIdsArr);});});
}
$list $query-orderBy(id, desc)-paginate($limit)输出goods列表包含所属的商家和商家所属的公司
$builder Goods::where(status,1);
if($request-filled(type)){$builder-where(type,$request-type);
}
//两种都可以
//$builder-with([business.company])-get();
$list $builder-with([businessfunction($query){$query-with(company);
}])-get();
文章转载自: http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.thjqk.cn.gov.cn.thjqk.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.tqsnd.cn.gov.cn.tqsnd.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.dpsgq.cn.gov.cn.dpsgq.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.hkng.cn.gov.cn.hkng.cn http://www.morning.playmi.cn.gov.cn.playmi.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.wiitw.com.gov.cn.wiitw.com