the following is the query sentence I wrote, but I am worried to death, but the function is realized. It is too hard to write. I repeat a lot. May I ask how to simplify it, or how to pass in the variable query, or if there are any other methods to simplify it? please give me an example, or the demonstration part, thank you
.public function postTreat(Request $request)
{
$_page = $request->input("_page"); //
$_path = $request->input("_path"); // path
$_sortType = $request->input("_sortType"); //
$_sales = $request->input("_sales"); //
$_priceSmall = $request->input("_priceSmall"); //
$_priceBig = $request->input("_priceBig"); //
$page=($_page-1)*4;
//
if(empty($_priceSmall)&&empty($_priceBig)){
//
if(empty($_sales)){
//
if($_sortType=="composite" || $_sortType==""){ //
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_up"){ //
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->orderBy("goods_price","asc") //
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_down"){ //
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->orderBy("goods_price","desc") //
->skip($page)
->take(4)
->get();
}else if($_sortType=="assess_down"){ //
// ,
$data = DB::table("shop_goods")
->leftJoin("shop_assess", "shop_goods.goods_id", "=", "shop_assess.assess_gcode")
->selectRaw("shop_goods.*,COUNT(shop_assess.assess_id) as assess_num")
->where("shop_goods.goods_cid",$_path)
->where("shop_goods.goods_status",1) // 0 1 2
->where("shop_goods.goods_state",0) // 0 1
->where("shop_goods.goods_recycle",0) // 0 1
->groupBy("shop_goods.goods_id")
->orderBy("assess_num","desc")
->get();
}else if($_sortType=="publish_new"){ //
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->orderBy("goods_time","desc") //
->skip($page)
->take(4)
->get();
}
}else{
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->orderBy("goods_num","desc") //
->skip($page)
->take(4)
->get();
}
}else{
//
if(empty($_sales)){
//
if($_sortType=="composite" || $_sortType==""){
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->whereBetween("goods_price",[$_priceSmall,$_priceBig]) //
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_up"){
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->whereBetween("goods_price",[$_priceSmall,$_priceBig]) //
->orderBy("goods_price","asc") //
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_down"){
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->whereBetween("goods_price",[$_priceSmall,$_priceBig]) //
->orderBy("goods_price","desc") //
->skip($page)
->take(4)
->get();
}else if($_sortType=="assess_down"){
$data = DB::table("shop_goods")
->leftJoin("shop_assess", "shop_goods.goods_id", "=", "shop_assess.assess_gcode")
->selectRaw("shop_goods.*,COUNT(shop_assess.assess_id) as assess_num") //
->where("shop_goods.goods_cid",$_path)
->where("shop_goods.goods_status",1) // 0 1 2
->where("shop_goods.goods_state",0) // 0 1
->where("shop_goods.goods_recycle",0) // 0 1
->whereBetween("shop_goods.goods_price",[$_priceSmall,$_priceBig]) //
->groupBy("shop_goods.goods_id")
->orderBy("assess_num","desc")
->get();
}else if($_sortType=="publish_new"){
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->whereBetween("goods_price",[$_priceSmall,$_priceBig]) //
->orderBy("goods_time","desc") //
->skip($page)
->take(4)
->get();
}
}else{
$data = DB::table("shop_goods")
->where("goods_cid",$_path)
->where("goods_status",1) // 0 1 2
->where("goods_state",0) // 0 1
->where("goods_recycle",0) // 0 1
->whereBetween("goods_price",[$_priceSmall,$_priceBig])
->orderBy("goods_num","desc")
->skip($page)
->take(4)
->get();
}
}
foreach($data as $key => $value){
if($value->goods_num>10000){
$value->goods_num = round(($value->goods_num)/10000,1).""; //
}
}
return $data;
}