Laravel关联模型查询

一,多表关联

文章表articles 和user_id,category_id关联

php 复制代码
//with()方法是渴求式加载,缓解了1+N的查询问题,仅需1+1次查询就能解决问题,可以提升查询速度。with部分没有就以null输出,所以可以理解为  多表  left join 查询
//has()和whereHas() 是基于关联关系去过滤模型的查询结果,如果关联模型不符合条件那整体就没有数据。所以可以理解为多表  inner join 查询。
  • 模型关系
php 复制代码
public function user(){
	return $this->belongsTo('App\Models\User','user_id','id');
}


//表2
 public function category(){
	return $this->belongsTo('App\Models\Category','category_id','id');
 }
  • 控制器
php 复制代码
//输出文章表中所有文章数据,如果符合用户id=1,分类name=科技 就输出关联的信息,没有就是null,类似left join 查询
$list = Article::with([
    'user'=>function($query){
        $query->where('id', '=', '1');
    },
    'category'=>function($query){
        $query->where('name', '=', '科技');
    },
    '模型定义的方法名'=>function($query){
     	$query->where('name', '=', '科技');
    }
])
->where('status',1)
->get();
php 复制代码
//输出文章表中符合条件的文章,需要满足用户id=1,分类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([
    'user'=>function($query){
        $query->where('id', '=', '1');
    },
    'category'=>function($query){
        $query->where('name', '=', '科技');
    }
])
->where('status',1)
->get();

实际使用

php 复制代码
//$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([
    'user'=>function($query){
        $query->where('id', '=', '1');
    },
    'category'=>function($query){
        $query->where('name', '=', '科技');
    }
])
->where('status',1)
->get();

二,多表多层级嵌套关联查询

一个company有多个store,一个store有多个goods

php 复制代码
$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列表包含所属的商家和商家所属的公司

php 复制代码
$builder = Goods::where('status',1);
if($request->filled('type')){
   $builder->where('type',$request->type);
}
//两种都可以
//$builder->with(['business.company'])->get();
$list = $builder->with(['business'=>function($query){
    $query->with('company');
}])->get(); 
相关推荐
dog shit2 小时前
web第十次课后作业--Mybatis的增删改查
android·前端·mybatis
emo了小猫2 小时前
Mybatis #{} 和 ${}区别,使用场景,LIKE模糊查询避免SQL注入
数据库·sql·mysql·mybatis
科技道人3 小时前
Android15 launcher3
android·launcher3·android15·hotseat
潘yi.7 小时前
NoSQL之Redis配置与优化
数据库·redis·nosql
zdkdchao7 小时前
hbase资源和数据权限控制
大数据·数据库·hbase
伤不起bb7 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
leo__5207 小时前
PostgreSQL配置文件修改及启用方法
数据库·postgresql
CYRUS_STUDIO7 小时前
FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码
android·安全·逆向
南風_入弦9 小时前
优化09-表连接
数据库·oracle
Snk0xHeart9 小时前
极客大挑战 2019 EasySQL 1(万能账号密码,SQL注入,HackBar)
数据库·sql·网络安全