MapMyMind
MapMyMind

Reputation: 153

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?

Upvotes: 0

Views: 490

Answers (3)

Saumya Rastogi
Saumya Rastogi

Reputation: 13709

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!

Upvotes: 0

Bara' ayyash
Bara' ayyash

Reputation: 1925

You can do laravel model query to check if there is any results, like this :

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

Upvotes: 2

Diego Vidal
Diego Vidal

Reputation: 1057

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
}

Upvotes: 0

Related Questions