将SQL查询转换为Laravel查询

时间:2016-12-27 10:18:25

标签: php sql laravel

我正在尝试通过查询检查c_start和c_end之间已存在的给定输入,但它无法正常工作。这是我的控制器..例如,如果第1人从10到12预订了房间,那么人们不能在同一天11到12之间或在现有预订时间之间的任何时间预订同一房间。

/* namespace App\booking; */

namespace App\Http\Controllers;

use App\booking;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use DB;
use App\Quotation;
use DateTime;

class RoombookController extends Controller {

    public function insert_check(Request $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 = array_sum($dbstime);
        $dbesum = array_sum($dbetime);

        $users = DB::table("bookings")
                 ->select("id")
                 ->where('bookdate', '=', $request->input('bookdate'))
                 ->where('roomname', '=', $request->input('roomname'))
                 ->where(function ($query){
                           $query->where('c_start', '<', $dbsum);
                           $query->orWhere('c_end', '>', $dbsum);
                         })
                 ->orWhere(function($query){
                             $query->where('c_start', '<', $dbesum);
                             $query->orWhere('c_end', '>', $dbesum);
                           });
        if ($users->count() == 0) {
            $booking = new 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->booked_by = Auth::user()->id;



            $smstime = explode(':', $startTime);
            $emstime = explode(':', $finishTime);


            $smstime[0] = $smstime[0] * 60;
            $emstime[0] = $emstime[0] * 60;

            $startsum = array_sum($smstime);
            $endsum = array_sum($emstime);


            $booking_time = $startTime . ' to ' . $finishTime;

            $booking->c_start = $startsum;
            $booking->c_end = $endsum;
            $booking->booking_time = $booking_time;

            $time = ($etime - $stime) / 60;
            $booking->total_duration = $time;

            $booking->save();

            return view("bookmessage");
        } else {
            return view("duplicateValue");
        }
    }

}

4 个答案:

答案 0 :(得分:1)

试试这段代码,让我知道它是否有效:

   $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();

答案 1 :(得分:1)

将查询更改为:

$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);
                }); 
            });

答案 2 :(得分:0)

您可以直接在laravel查询构建器中运行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))

$ result变量包含您的输出。您必须使用数据库类

use DB;

答案 3 :(得分:0)

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

 $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();