Atef95's avatar

Store business hours of a company

Hey guys I would like to store business hours of a company during all the week..

I'm wondering what is the best way to do it?

something like :

day - open_hour - close_hour - is_closed

if I create a static form then what's the best way to store it in the back ?

do I have to create 7 instances manually ?

0 likes
8 replies
topvillas's avatar

Do you really need to store them in a database?

Sinnbeck's avatar

Personally I would add a row for each day to a table. Something like this (and make company/day unique)

id | company_id | day | open_hour | close_hour | closed

In migration

$table->unique(['company_id', 'day']);
1 like
midnightcipher's avatar

Does this need to be stored in a database as you will need to query them each time you want them which is not good for performance?

You could always put this into your config/app.php as an array

   'store.hours' => [
        'Monday' => '9am-5pm',
        'Tuesday' => '9am-5pm',
        'Wednesday' => '9am-5pm',
        'Thursday' => '9am-5pm',
        'Friday' => '9am-5pm',
        'Saturday' => 'Closed',
        'Sunday' => 'Closed',
    ],

then inside your blade template, you could call them and loop over them.

<ul>
    @foreach(Config::get('store.hours') as $day => $hours)
        <li>{{ $day }} - {{ $hours }}</li>
    @endforeach
</ul>

or if you want to access it inside a controller just do the following:

$storeHours = Config::get('store.hours')

If you need them in a database then I recommend the following structure:

"id" integer NOT NULL PRIMARY KEY,
"store_id" integer NOT NULL FOREIGN KEY REFERENCES "stores",
"day" integer NOT NULL,
"open_time" time,
"close_time" time

You don't need an is_closed column because you can just leave the open_time or close_time nullable. and do a statement to check on your blade template.

@foreach($store->hours as $store)

    {{ $store->day }} - {{ !empty($store->hours) ? $store->hours : '' }}

@endforeach

Check out ternary's for more information if you have not worked with them before :) https://stackoverflow.com/questions/25284344/ternary-in-laravel-blade

2 likes
Atef95's avatar

Hey guys thank you for all your responses.

but what I meant is the way I code the form in the view...

do I have to store them in an array ? or seperatly ?

for example

<label> Monday </label>
<input  name="mondayStartTime"> </input>

<label> Tuesday </label>
<input  name="tuesdayStartTime"> </input>


<label> Wedneday </label>
<input  name="wednedayStartTime"> </input>

..etc

or is this possible ?

<label> Monday </label>
<input  name="startTime[0]"> </input>

<label> Tuesday </label>
<input  name="startTime[1]"> </input>


<label> Wedneday </label>
<input  name="startTime[2]"> </input>

..etc

audunru's avatar

From experience, store each open and close time separately in the database. That way you can query easily for stores open now, doing things like closing in 30 minutes, etc. Make it human readable in the front end, not when you store it. As for naming the inputs, I dont know... perhaps using the day names makes it a little more explicit, otherwise you can have people looking at your code wonder if 0 is Monday or Sunday. But I think you can feel quite confident if you make it an array with 7x2 entries, thats not very likely to change.

Please or to participate in this conversation.