Php 将SQL查询转换为Laravel查询

Php 将SQL查询转换为Laravel查询,php,sql,laravel,Php,Sql,Laravel,我试图通过查询检查给定的输入是否已经存在于c_开始和c_结束之间,但它不起作用。这是我的控制器。。例如,如果一个人在10到12之间预订了一个房间,那么他就不能在11到12之间的同一日期或现有预订时间之间的任何时间预订同一个房间 /*命名空间应用程序\预订*/ 名称空间App\Http\Controllers; 使用应用程序\预订; 使用\Http\Request; 使用Illumb\Database\Elount\Model; 使用Illumb\Support\Facades\Auth; 使用

我试图通过查询检查给定的输入是否已经存在于c_开始和c_结束之间,但它不起作用。这是我的控制器。。例如,如果一个人在10到12之间预订了一个房间,那么他就不能在11到12之间的同一日期或现有预订时间之间的任何时间预订同一个房间

/*命名空间应用程序\预订*/
名称空间App\Http\Controllers;
使用应用程序\预订;
使用\Http\Request;
使用Illumb\Database\Elount\Model;
使用Illumb\Support\Facades\Auth;
使用数据库;
使用App\quote;
使用日期时间;
类RoombookController扩展控制器{
公共功能插入检查(请求$Request){
$roomname=$request->input('roomname');
$book=$request->input('bookdate');
$dbstime=explode(“:”,$request->input('starttime');
$dbetime=explode(“:”,$request->input('endtime');
$dbstime[0]=$dbstime[0]*60;
$dbetime[0]=$dbetime[0]*60;
$dbsum=数组和($dbstime);
$dbesum=array_sum($dbetime);
$users=DB::表格(“预订”)
->选择(“id”)
->其中('bookdate','=',$request->input('bookdate'))
->其中('roomname','=',$request->input('roomname'))
->其中(函数($query){
$query->where('c_start','',$dbsum);
})
->orWhere(函数($query){
$query->where('c_start','',$dbesum);
});
如果($users->count()==0){
$booking=新预订;
$booking->bookdate=$request->input('bookdate');
$booking->roomname=$request->input('roomname');
$booking->starttime=$starttime=$request->input('starttime');
$booking->endtime=$finishTime=$request->input('endtime');
$booking->purpose=$request->input('purpose');
$booking->booking_by=Auth::user()->id;
$smstime=explode(“:”,$startTime);
$emstime=explode(“:”,$finishTime);
$smstime[0]=$smstime[0]*60;
$emstime[0]=$emstime[0]*60;
$startsum=数组和($smstime);
$endsum=数组和($emstime);
$booking_time=$startTime.'to'.$finishTime;
$booking->c_start=$startsum;
$booking->c_end=$endsum;
$booking->booking\u time=$booking\u time;
$time=($etime-$stime)/60;
$booking->total_duration=$time;
$booking->save();
返回视图(“bookmessage”);
}否则{
返回视图(“重复值”);
}
}
}

请尝试此代码,并告诉我它是否有效:

   $whereCondition = array('bookdate'=>$request->input('bookdate'),'roomname'=>$request->input('roomname'));
$users = DB::table("bookings") 
               ->select("id") 
               ->where($whereCondition) 
               ->where(function ($query use $dbsum){
                   $query->where('c_start','<',$dbsum);
                   $query->orWhere('c_end','>',$dbsum);
               })
               ->orWhere(function($query use $dbesum){
                  $query->where('c_start','<',$dbesum);
                  $query->orWhere('c_end','>',$dbesum);
               })
               ->get();
$whereCondition=array('bookdate'=>$request->input('bookdate'),'roomname'=>$request->input('roomname');
$users=DB::表格(“预订”)
->选择(“id”)
->where($whereCondition)
->其中(函数($query use$dbsum){
$query->where('c_start','',$dbsum);
})
->orWhere(函数($query use$dbesum){
$query->where('c_start','',$dbesum);
})
->get();
将查询更改为:

$users = DB::table("bookings") 
            ->select("id") 
            ->where('bookdate', '=', $request->get('bookdate')) 
            ->where('roomname', '=', $request->get('roomname'))
            ->where(function ($query) use ($dbsum, $dbesum){
                $query->where(function ($query) use ($dbsum){
                    $query->where('c_start','<',$dbsum);
                    $query->Where('c_end','>',$dbsum);
                 })
                 ->orWhere(function($query) use($dbesum){
                    $query->where('c_start','<',$dbesum);
                    $query->orWhere('c_end','>',$dbesum);
                }); 
            });
$users=DB::table(“预订”)
->选择(“id”)
->其中('bookdate','=',$request->get('bookdate'))
->其中('roomname','=',$request->get('roomname'))
->其中(函数($query)使用($dbsum,$dbesum){
$query->where(函数($query)使用($dbsum){
$query->where('c_start','',$dbsum);
})
->orWhere(函数($query)use($dbesum){
$query->where('c_start','',$dbesum);
}); 
});

您可以直接在laravel query builder中运行sql查询。您可以尝试以下操作:

$sql = "SELECT * FROM `bookings` WHERE `bookdate`='2016-12-27' AND `roomname`='3' AND (($dbsum>`c_start` AND $dbsum<`c_end`) OR ($dbesum>`c_start` AND $dbesum<`c_end`))";

$result = DB::select(DB::raw($sql))

这是我得到正确结果的查询

 $whereCondition = array('bookdate'=>$request->input('bookdate'),'roomname'=>$request->input('roomname'));
    $users = DB::table("bookings") 
           ->select("id") 
           ->where('bookdate', '=', $request->get('bookdate')) 
           ->where('roomname', '=', $request->get('roomname'))
           ->whereRaw(DB::raw("((c_start<$dbsum and c_end>$dbsum) or (c_start<$dbesum and c_end>$dbesum ))"))
           ->get();
$whereCondition=array('bookdate'=>$request->input('bookdate'),'roomname'=>$request->input('roomname');
$users=DB::表格(“预订”)
->选择(“id”)
->其中('bookdate','=',$request->get('bookdate'))
->其中('roomname','=',$request->get('roomname'))
->其中raw(DB::raw(((c_start$dbsum)或(c_start$dbesum)))
->get();

Undefined variable:dbsum我不知道为什么会出现此错误,因为我只做了几次编辑,所以您的查询是否运行良好。请以$dbsumi的形式公布您得到的值,如果我输入的开始时间为10,那么dbsum将为10*60=600,并且由dd()表示我在查询之前得到该值,但在查询中找不到该值在更改查询并尝试这两个变量之前是否出现任何错误在同一控制器上的任何其他函数中是否存在同名的其他变量,还要检查生成错误的行号。分析错误:语法错误,意外的“}”这是显示..但我看不到像这样的错误我的错,我错过了;在}之后)。请再次检查。始终显示我的房间已预订…无论我在开始时间和结束时间输入什么
use DB;
 $whereCondition = array('bookdate'=>$request->input('bookdate'),'roomname'=>$request->input('roomname'));
    $users = DB::table("bookings") 
           ->select("id") 
           ->where('bookdate', '=', $request->get('bookdate')) 
           ->where('roomname', '=', $request->get('roomname'))
           ->whereRaw(DB::raw("((c_start<$dbsum and c_end>$dbsum) or (c_start<$dbesum and c_end>$dbesum ))"))
           ->get();