Php 在Laravel的关系中运行的条件查询

Php 在Laravel的关系中运行的条件查询,php,laravel,Php,Laravel,这里我有一个在关系中查询的代码,如果用户向我发送变量,我只希望执行一部分,如果没有,则查询根本没有运行这里是我在代码中注释的代码,我希望我的条件: $bed_count = $request->get('bed_count'); $data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_da

这里我有一个在关系中查询的代码,如果用户向我发送变量,我只希望执行一部分,如果没有,则查询根本没有运行这里是我在代码中注释的代码,我希望我的条件:

$bed_count = $request->get('bed_count');
$data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_date){
$query->whereDate('from_date', '<=', $from_date);
                    $query->whereDate('to_date', '>=', $to_date);
         },'accommodationRoomsLimited' => function($q) use($bed_count){
          //here is I want my query to be executed if the user sends bed_count if not the code should skip this part
          $q->orWhere('bed_count',$bed_count);
        }])
$bed_count=$request->get('bed_count');
$data=住宿::使用(['city','AccountationRoomSlimited.roomPricingHistorySearch'=>函数($query)使用($from\u date,$to\u date){
$query->whereDate('from_date','=',$to_date);
},“住宿房间Slimited”=>功能($q)使用($bed_count){
//如果用户发送bed_count,我希望执行我的查询。如果不是,代码应该跳过这一部分
$q->orWhere('bed_count',$bed_count);
}])

不清楚您想要实现什么目标

  • 如果您想随时装入
    住宿房间

    但当用户提供了床位数时,您需要使用此床位数筛选那些
    住宿房间的床位
  • 如果是这样的话

    $bed_count=$request->get('bed_count');
    $data=住宿::带([
    "城市",,
    “住宿房间Slimited.roomPricingHistorySearch”=>函数($query)使用($from\u date,$to\u date){
    $query->whereDate('from_date','=',$to_date);
    },
    “住宿房间Slimited”=>功能($q)使用($bed_count){
    如果($bed_计数){
    $q->where('bed_count',$bed_count);
    }
    }
    ]);
    
  • 如果您只想在用户提供床位数的情况下立即加载
    住宿房间slimited
  • 然后

    $bed_count=$request->get('bed_count');
    $data=住宿::带([
    "城市",,
    “住宿房间Slimited.roomPricingHistorySearch”=>函数($query)使用($from\u date,$to\u date){
    $query->whereDate('from_date','=',$to_date);
    },
    ])
    ->when($bed\u count,函数($query,$bed\u count){
    $query->with([
    “住宿房间Slimited”=>功能($q)使用($bed_count){
    $q->where('bed_count',$bed_count);
    }
    ]);
    });
    
    不清楚您想要实现什么目标

  • 如果您想随时装入
    住宿房间

    但当用户提供了床位数时,您需要使用此床位数筛选那些
    住宿房间的床位
  • 如果是这样的话

    $bed_count=$request->get('bed_count');
    $data=住宿::带([
    "城市",,
    “住宿房间Slimited.roomPricingHistorySearch”=>函数($query)使用($from\u date,$to\u date){
    $query->whereDate('from_date','=',$to_date);
    },
    “住宿房间Slimited”=>功能($q)使用($bed_count){
    如果($bed_计数){
    $q->where('bed_count',$bed_count);
    }
    }
    ]);
    
  • 如果您只想在用户提供床位数的情况下立即加载
    住宿房间slimited
  • 然后

    $bed_count=$request->get('bed_count');
    $data=住宿::带([
    "城市",,
    “住宿房间Slimited.roomPricingHistorySearch”=>函数($query)使用($from\u date,$to\u date){
    $query->whereDate('from_date','=',$to_date);
    },
    ])
    ->when($bed\u count,函数($query,$bed\u count){
    $query->with([
    “住宿房间Slimited”=>功能($q)使用($bed_count){
    $q->where('bed_count',$bed_count);
    }
    ]);
    });
    
    谢谢你为我节省了时间!!!是当用户给出床位数时告诉查询加载时??是
    when()
    函数第一个参数是一个条件,第二个参数是条件通过时要执行的操作。谢谢,这节省了我的时间!!!是当用户给出床位数时告诉查询加载时??是
    when()
    函数第一个参数是一个条件,第二个参数是条件通过时要做的事情。