Laravel 两表关联查询

问题描述:

两表关联 商品表,关联库存表, 现 库存表 反向关联商品表,查询商品名字, 但会查询出很多为空的数据
未使用查询条件结果如图
\

模型关联代码如下:

class GoodsSpec extends Model
    {
        protected $guarded = [];
        protected $table = 'g_specification';

        // 商品模型关联商品表
        public function goods(){
            return $this->belongsTo(Goods::class,'g_product_id','product_id');
        }
    }

查询代码如下

// 商品库存日志
class GoodsStockLogController extends Controller
{
    use LayuiTableController;
    public function lists(Request $request){
        DB::connection()->enableQueryLog();#开启执行日志
        $dd =  GoodsSpec::with(['goods' => function($query) use($request){
            if ($request->has('search.name')){
                $query->where('name', $request->search['name'] )->select('name','product_id');
            }else{
                $query->select('name','product_id');
            }
        }])
            ->offset($this->params($request))
            ->limit($request->limit)
            ->get()->toArray();

//        dd(DB::getQueryLog());
//        dd($dd);
        $count =  GoodsSpec::with(['goods' => function($query) use($request){
            if ($request->has('search.name')){
                $query->where('name', $request->search['name'] );
            }
        }])->count();

        return asTableData($dd,$count);
    }
}

期望的查询结果:


现在查询结果 会出现 图中 X掉的 数据 , 如果用 DB查询是可以获取到预期答案,但是享受不到验证器的便利...
谢谢大家的帮助

表结构

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

@早起的虫子

i Try it out。。。
file
file

但是使用方面待提升, 以为 很多 搞不清楚放在前面和放在后面, 位置放的不对 结果也不一样,, 谢谢你

5年前 评论
讨论数量: 5

根据文档
file
你想要查询至少拥有一条对应商品记录的库存,所以你应该这么写:
$dd = GoodsSpec::has('goods')
->with(['goods' => function($query) use($request){
if ($request->has('search.name')){
$query->where('name', $request->search['name'] )->select('name','product_id');
}else{
$query->select('name','product_id');
}
}])
->offset($this->params($request))
->limit($request->limit)
->get()->toArray();

5年前 评论

@早起的虫子 不行的

然后我用了一个非常不够优雅的办法

   /*    $sql = GoodsSpec::whereHas('goods',function ($query) use ($request){
            if ($request->has('search.name')){
                $query->where('name', $request->search['name'])->orWhere('product_id',$request->search['name']);
            }
        });*/

            $dd = $sql->offset($this->params($request))
            ->limit($request->limit)
            ->get()->map(function ($item,$value){
                 $item['goods'] = Goods::where('product_id',$item['g_product_id'])
                     ->orWhere('product_id',$item['g_product_id'])
                     ->select('name','id')->first();
                 return $item;
            }); 

楼下有给我答案是文档的,可能自己看不够仔细, 我把文档在去看一下去

5年前 评论

@早起的虫子

i Try it out。。。
file
file

但是使用方面待提升, 以为 很多 搞不清楚放在前面和放在后面, 位置放的不对 结果也不一样,, 谢谢你

5年前 评论

@taobali32 我忽略了你这个查询商品的时候要搜索名字的。。。那的确要用whereHas,单纯用has是通过模型关联的字段进行查询的,所以还是查不出来

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!