artyak
artyak

Reputation: 17

Why data from the table is not displayed?

I use laravel, the maximum value from the database should be displayed in my block. Previously, everything worked fine, the data was displayed up-to-date, but now for some reason the data is not displayed.

My Controller with display function:

    public function bust()
    {
        parent::setTitle('Crash game | ');
        $users = User::count();

$max_x = DB::table('bust_history')->where('created_at', '>=', Carbon::today())->max('bust_number');



        return view('pages.crash', compact('users','max_x'));
    }

My view file:

<div class="header">
            <div> Max X </div>
            <div class="help"> <small>for last 24 hours</small> </div>
        </div>
        <div class="body ">
            <div style="color:#48B3CD;font-size:45px;position:center;">{{$max_x}}x</div>
        </div>

Here is screenshot from my DB

And a screenshot of what is displayed on the site: here

Who can tell me, what could be the problem?

Upvotes: 0

Views: 129

Answers (1)

Anthony Aslangul
Anthony Aslangul

Reputation: 3857

[EDIT]

You can see the whole debug process below and in the comments. The right answer, the cause of the bug, was the type of bust_number column. It was a VARCHAR type, which caused incorrect results when trying to get the maximum value. Changing it to FLOAT or DOUBLE fixed the issue, since these types can be manipulated as numbers instead of strings by MySql.


It is hard to give an answer since this can be caused by many things, but here are the common causes of your problem:

  • You use the wrong database or the table is empty. Please double check that
DB::table('bust_history')->pluck('bust_number');

gives you the data you expect.

  • There is a problem in PHP or Carbon configuration and Carbon::today(); is not really today (as i'm writing, 2020-01-09 00:00:00). If it is right, does
DB::table('bust_history')->where('created_at', '>=', Carbon::today())->pluck('bust_number');

gives you a list of bust_number ?

If the problem persists and you give additional details after these checks, i could edit my answer with more things to check. But in my experience, you should check what seems to be too obvious to be checked. At some point, you'll find something that's not normal.

Also, don't even use your view, just dd($max_x); right after the query, so you can be sure nothing interfers with your result.

Upvotes: 1

Related Questions