我想创建从MySql数据库中搜索数据的函数。我的搜索表单包含三个字段。进入这些领域的用户可以放一些文字/值或者不像正常的搜索形式.. 但是我遇到了一些问题 - laravel并没有回复我的任何价值观...... 我用:
Input::get('bus', 'Business.ID');
将用户插入的空值替换为每个值 接下来在:
-> where('Business.ID', '=', $bus)
我尝试搜索值... 但不幸的是,它总是在我的json中返回空表..我怎样才能达到目标? 我试过的整个代码和注释行
public function searchVoucher(){
//$input = Input::all();
// if(!isset($input['business'])){
// $bus = 'Business.ID';
// }
$bus = Input::get('bus', 'Business.ID');
$company = Input::get('company', 'Companies.Comp_Name');
$city = Input::get('city', 'Companies.City');
// SELECT * FROM Vouchers
// INNER JOIN Companies
// ON Vouchers.ID_Company = Companies.ID
// Inner JOIN Business
// on Companies.Business_Id = Business.ID
// where Business.ID = Business.ID
$results = DB::table('Vouchers')
//-> select('Vouchers.ID', 'URL as Url', 'Vou_Name as Name', 'Comp_Name', 'Discount', 'Quantity', 'Date')
-> Join('Companies', 'Vouchers.ID_Company', '=', 'Companies.ID')
-> Join('Business', 'Companies.Business_ID', '=', 'Business.ID')
-> where('Business.ID', '=', $bus)
//-> whereRaw("Business.ID =$bus")// and Companies.Comp_Name =$company and Companies.City = $city")
//-> whereRaw("Companies.Comp_Name =$company")
-> where('Companies.Comp_Name', '=', $company)
-> get();
//if($results){
return Response::json(['SearchList' => $results,'bus'=>$bus, 'success' => 1]);
// }else {
// return Response::json(['SearchList'=>'No vouchers found', 'success'=>0]);
// }
}
答案 0 :(得分:0)
你的问题在这里
Input::get('bus', 'Business.ID');
将用户插入的空值替换为下一个值:
-> where('Business.ID', '=', $bus)
您正在为输入定义默认值,因此此查询;
$results = DB::table('Vouchers')
-> Join('Companies', 'Vouchers.ID_Company', '=', 'Companies.ID')
-> Join('Business', 'Companies.Business_ID', '=', 'Business.ID')
-> where('Business.ID', '=', $bus)
-> where('Companies.Comp_Name', '=', $company)
-> get();
这可能是这样的:
$results = DB::table('Vouchers')
-> Join('Companies', 'Vouchers.ID_Company', '=', 'Companies.ID')
-> Join('Business', 'Companies.Business_ID', '=', 'Business.ID')
-> where('Business.ID', '=', 'Business.ID')
-> where('Companies.Comp_Name', '=', 'Companies.Comp_Name')
-> get();
这是你需要做的,增量查询:
$myQuery = DB::table('Vouchers')
->Join('Companies', 'Vouchers.ID_Company', '=', 'Companies.ID')
->Join('Business', 'Companies.Business_ID', '=', 'Business.ID');
if (Input::has('bus'))
{
$myQuery = $myQuery-> where('Business.ID', '=', Input::get('bus'));
}
if (Input::has('company'))
{
$myQuery = $myQuery> where('Companies.Comp_Name', '=', Input::get('company'));
}
if (Input::has('city'))
{
$myQuery = $myQuery> where('Companies.City', '=', Input::get('city'));
}
$results = $myQuery->get();
您还可以将所有这些提取到雄辩的范围中,