您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
laravel的hasMany筛选(where)
发布时间:2019-12-08 11:08:51编辑:雪饮阅读()
有字典模型如:
class TdDictTop extends Model
{
protected $table = 't_dt_dict_top';
public function childs()
{
return $this->hasMany(TdDictChild::class,'top_id',"id");
}
}
有子字典模型如:
class TdDictChild extends Model
{
protected $table = 't_dt_dict_child';
}
从上面可以看到字典模型hasMany了子字典模型,那么如果要从字典模型调用hasMany时需要对hasMany的结果再次筛选,但又不想通过循环实现,该如何实现?
$region=TdDictTop::with(['childs' => function ($query) {
$query->where("status",1);
}])->where(["code"=>"region","status"=>1])->get();
从上面可以看到对于该需求用了with,没有办法with虽然看起来比较不优雅,但要对hasMany进行筛选则只能这样
关键字词:laravel,hasMany,筛选,where
上一篇:免数据线用安卓向psv传递文件
下一篇:CI简介与实现简单的MVC