1、连表查询:select * from goods left join shop on goods.shopid=shop.shopid;
DB::table('goods') ->leftJoin('shop', 'shop.shopid', '=', 'goods.shopid') ->get();
2、or查询:select * from goods where (good_name like '%$keyWord%' or shop_name like '%$keyWord%' )
DB::table('goods')
->where(function ($query) use ($keyWord) { $query->where('good_name', 'like', "%{$keyWord}%")->orWhere('shop_name', 'like', "%{$keyWord}%"); })
->get();
3、查询结果转为数组:toArray
DB::table('goods')->get()->toArray();
4、打印方法体里所有的sql语句:
DB::enableQuerylog(); $this->test(); var_dump(DB::getQueryLog());
5、leftJoin里有多个条件:
->leftJoin('wish_record', function ($join) use ($userId) { $join->on('wish_record.s_g_id', '=', 'wish_shop_goods.rc_id')->where('wish_record.user_id', $userId); });