skip to Main Content

im currently making this queue web, so i want to select the longest overall queue time. im able to select the longest queue time per category but somehow when i want to retrieve the longest overall queue time, my model doesnt select the correct data.

Controller to select the queue time

This part to take overall longest queue time

$longestQ = queueModel::max('wait_time');

this part to take the longest queue time per category

$longestQ1 = queueModel::where('category',$categories[0]->QName)->whereDate('created_at','=',now())->max('wait_time');

this is the result of the overall queue time

"7:27" // appHttpControllersSpvController.php:606

and this is the result of the longest queue time of 1 category, it should be the longest overall

"36:59" // appHttpControllersSpvController.php:929

could anybody explain this the way a dummy could undderstand pls.

if you need more part of the code, i would be happy to provide. thanks in advance

Picture for context
The table showing overall statistics

i have tried to convert the column content from string to time but still doesnt work,
also i’ve tried using some query builder but still giving the same result as the eloquent

2

Answers


  1. It will return the maximum value for wait_time across all rows in the table, rather than just the rows for the current day. To select the longest overall queue time for the current day, update your query to filter by the current day using the whereDate :

    $longestQ = queueModel::whereDate('created_at', '=', now())
        ->max('wait_time');
    

    If wait_time is stored as a string, you may need to convert it to a numeric format using attribute casting.

    Login or Signup to reply.
  2. If your wait_time column is of type VarChar (i.e. string), then the database will compare its values as strings. It has no idea of the meaning of those strings, it just looks at them character by character.

    Given the following list:

    • "00000000"
    • "7:27"
    • "5 years"
    • "36:59"

    The maximum value, i.e. the one that comes last in alphabetical order, is "7:27", because any string starting "7" will come after any string starting "0", "3", or "5".

    To avoid this, you need to do one of two things:

    • Use a more appropriate column type in the design of your database. If the database knows that you intend these as minutes and seconds, it can compare them appropriately. It may normalise a value like 75:20 to 1:15:20, but you can format it back on the way out if you want.
    • Format your string values so that they compare lexicographically, e.g. by having a fixed width padded on the left with zeros. Given the strings "007:27" and "036:59", the maximum (i.e. sorted last) will be "036:59", as you wanted.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search