簡體   English   中英

如何在Laravel中顯示使用來自同一數據庫表的兩個不同值的圖?

[英]How do I display a graph in Laravel which uses two different values from the same database table?

我正在嘗試制作一個laravel應用程序,其中的折線圖顯示了與特定日期相對應的已登錄用戶的血糖水平。 我目前正在使用ConsoleTV軟件包,但可以使用其他選項。

我嘗試了幾個不同的查詢,但似乎無法准確顯示我想要的內容。 在Y軸上而不是顯示血糖水平,它只是計算當天的記錄數。

我的控制器:

public function chart()
{
   // $bloodSugar = BloodSugar::where(\DB::raw("(DATE_FORMAT(created_at,'%D'))"),date('D'))->where('user_id', Auth::id())->get();

    $bloodSugar = BloodSugar::where('user_id', Auth::id())->get();


    $chart = Charts::database($bloodSugar, 'line', 'highcharts')
        ->title('Blood Sugars')
        ->elementLabel('Blood Sugar Level')
        ->groupByDay();

    return view ('bloodsugars.index', compact('chart'));
}

我的用戶模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function reviews()
    {
        return $this->hasMany(BloodSugar::class);
    }
}

我的血糖模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BloodSugar extends Model
{
    protected $fillable = ['date', 'time', 'blood_sugar_level', 'scenario', 'user_id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

我的用戶數據庫表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->nullable();
            $table->date('dob')->nullable();
            $table->string('diabetic_type')->nullable();
            $table->string('other_info')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('provider')->nullable();
            $table->string('provider_id')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

我的血糖表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBloodSugarsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blood_sugars', function (Blueprint $table) {
            $table->increments('id');
            $table->date('date');
            $table->time('time');
            $table->double('blood_sugar_level');
            $table->text('scenario');
            $table->integer('user_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('blood_sugars');
    }
}

我的圖表當前輸出當天記錄的血糖數量計數,而不是實際的血糖水平本身。

當您使用的庫版本過時並且與當前文檔不匹配時,很難提供幫助。 我能做的最好的就是為您提供一個基於當前文檔的解決方案。

請查看Eloquent示例 ,因為這就是我正在修改的示例。 這些將插入控制器方法的中間。 下面,我們僅給出一天中每個時間的折線圖。

$chart = new SampleChart;

$chart->labels($bloodSugar->map(function (Bloodsugar $reading) {
    return Carbon::parse($reading->date . ' ' . $reading->time);
}));

$chart->dataset('Bloodsugar Readings', 'line', $bloodSugar->pluck('blood_sugar_level'));

如果要按天報告平均血糖,則只需使用“ Collection方法對數據進行一點轉換,然后再將其傳遞給圖表庫:

// Keys are dates and values are average levels for those dates.
$averageBloodSugarByDay = $bloodSugar->groupBy('date')
    ->map(function (Bloodsugar $readingsForDate) {
        return $readingsForDate->avg('blood_sugar_level');
    });

$chart = new SampleChart;

$chart->labels($averageBloodSugarByDay->keys());
$chart->dataset('Bloodsugar Readings', 'line', $averageBloodSugarByDay->values());

如果您不清楚這些摘要中發生了什么,請參考Laravel Collection文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM