Laravel 5.5无法使用ConsoleTV的图表包呈现动态图表

时间:2019-06-26 03:53:43

标签: javascript php mysql laravel dynamic

为什么我不能生成来自数据库的数据?我可以渲染图表,但没有显示数据。但是,当我转储数据时,它向我显示了数据数组。帮助将不胜感激。谢谢。图表输出就在这里:https://imgur.com/a/eyCVxSn

PS:我想在第二个数据库中获取数据。这就是为什么模型中有“ mysql2”的原因。

GraphController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;
use View;
use App\News;
use Charts;
use App\Graph;

class GraphController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {



        $graph = Charts::database(Graph::find(1),'line','highcharts')
        ->title('Tokens')
        ->ElementLabel('Coins Sold')
        ->Responsive(true)
        ->dimensions(1000,500)
        ->data(Graph::find(1));

        dd($graph);

        $news = News::all();


        // //dd($news);
        return View::make('coin.news', compact('graph','news'));
    }
}

Graph.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;
use Cache;
use Charts;


class Graph extends Model
{
    //

    protected $connection = 'mysql2';
    protected $table = 'ico_stages';



}


graph.blade.php




@section('graph')



        {!! $graph->render() !!}



@endsection

app.blade.php

stylesheets....

{!! Charts::assets() !!}

@yield('graph')

news...

scripts...

当我使用dd($ graph)时,它显示了这一点。我还可以在图表中看到要渲染的数据。

Database {#299 ▼
  +data: Graph {#306 ▼
    #connection: "mysql2"
    #table: "ico_stages"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:18 [▼
      "id" => 1
      "name" => "Stage 1"
      "start_date" => "2019-05-11 21:29:00"
      "end_date" => "2019-11-30 21:29:00"
      "total_tokens" => 10000000
      "base_price" => 2.0
      "min_purchase" => 100
      "max_purchase" => 10000
      "soft_cap" => 0
      "hard_cap" => 0
      "display_mode" => "normal"
      "private" => 0
      "user_panel_display" => 0
      "sales_token" => 1496.0
      "sales_amount" => 2720.0
      "status" => "active"
      "created_at" => "2019-05-12 05:29:57"
      "updated_at" => "2019-06-19 06:02:55"
    ]
    #original: array:18 [▼
      "id" => 1
      "name" => "Stage 1"
      "start_date" => "2019-05-11 21:29:00"
      "end_date" => "2019-11-30 21:29:00"
      "total_tokens" => 10000000
      "base_price" => 2.0
      "min_purchase" => 100
      "max_purchase" => 10000
      "soft_cap" => 0
      "hard_cap" => 0
      "display_mode" => "normal"
      "private" => 0
      "user_panel_display" => 0
      "sales_token" => 1496.0
      "sales_amount" => 2720.0
      "status" => "active"
      "created_at" => "2019-05-12 05:29:57"
      "updated_at" => "2019-06-19 06:02:55"
    ]
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }
  +date_column: "created_at"
  +date_format: "l dS M, Y"
  +month_format: "F, Y"
  +hour_format: "D, M j, Y g A"
  +language: "en"
  +preaggregated: false
  +aggregate_column: null
  +aggregate_type: null
  +value_data: []
  +id: null
  +customId: null
  +type: "line"
  +library: "highcharts"
  +title: "Tokens"
  +element_label: "Coins Sold"
  +labels: []
  +values: []
  +colors: []
  +responsive: true
  +gauge_style: "left"
  +view: null
  +region: "world"
  #suffix: ""
  +container: ""
  +credits: false
  +loader: true
  +loader_duration: 500
  +loader_color: "#000000"
  +background_color: "inherit"
  +template: "material"
  +one_color: false
  +legend: true
  +x_axis_title: false
  +y_axis_title: null
  +script: ""
  +html: ""
  +export: null
  +"height": 500
  +"width": 1000
}

1 个答案:

答案 0 :(得分:1)

在图形模板中呈现数据

$graph = view('graph')->with('graph', $graph); 

并将渲染的数据返回新闻

return view('coin.news', compact('graph','news'));

最终密码

    $graph = Charts::database(Graph::find(1),'line','highcharts')
    ->title('Tokens')
    ->ElementLabel('Coins Sold')
    ->Responsive(true)
    ->dimensions(1000,500)
    ->data(Graph::find(1));



    $news = News::all();

    $graph = view('graph')->with('graph', $graph); 

    return view('coin.news', compact('graph','news'));