Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hidayat3676's avatar

How to get data from table in date wise and display it on a chart based on hour of that day

following is my code

Migration
  public function up()
    {
        Schema::create('panels', function (Blueprint $table) {
            $table->increments('id');
            $table->string('serial');
            $table->decimal('longitude', 9, 6);
            $table->decimal('latitude', 8, 6);
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('one_hour_electricities', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('panel_id');
            $table->foreign('panel_id')->references('id')->on('panels')->onDelete('cascade');
            $table->integer('kilowatts');
            $table->datetime('hour');
            $table->timestamps();
        });
    }

i want to get the data from the second migration and show it on chart hourly basis of each day using specific panel id. what type of query will i need to execute to achieve this.

0 likes
2 replies
igaster's avatar

You could use aggregate SQL functions and write a custom query like

SELECT COUNT(*) FROM one_hour_electricities
LEFT JOIN panels ON panel_id = panels.id
WHERE panels.id = xxx
GROUP_BY HOUR(one_hour_electricities.hour)

This will give you the count of rows per hour.

This should not be difficult to write it in Eloquent...

Please or to participate in this conversation.