简体   繁体   中英

Two unique values in column Laravel 5.3

I'm making a reservation system for a media library, the goal is to book a room for a certain time. There are 8 time slots in total with a start_time & end_time.

The user has to fill in a date and I have to check what time slots are still available at that date.

So, for example.. there can only be one row in the database that contains date: 2016-12-08 time_slot: 3.

How do I check if this row exists in my database using Eloquent?

您可以执行laravel模型查询以检查是否有任何结果,例如:

$data = Model::where('date','2016-12-08 ')->where('time_slot', 3)->count();

Assuming you have a slot table with the fields you are sharing. Slot table: id, date, time_slot...

$your_date = 2016-12-08;
$date = date('Y-m-d', strtotime($your_date));
$time_slot: 3;

$slot = Slot::where('date', $date)->where('slot', $time_slot)->first();

if($slot == null) {
  // book can be made
}
else {
  // already booked
}

You can take the help pf Laravel Query Builder's exists for checking the if the fields are present in database like this:

$exists = Model::whereDate('date', '2016-12-08')
                ->where('time_slot', 3)
                ->exists();

Here exists returns a boolean value, which depends upon the existence of the query above!

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM