Laravel显示来自另一个数据库连接的图表

时间:2019-06-19 04:53:26

标签: javascript php mysql laravel chart.js

如何从另一个数据库连接(.env文件中的“ mysql2”)在laravel中生成图表,可能是饼图或甜甜圈图?我知道如何从主数据库生成图表。但是这次我没有迁移第二个数据库,因为它已经具有表和数据。我可以使用“ dd”命令获取数据,但是如果生成图表,则是另一回事。请帮忙我的作业。

这是我的 HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;


use Charts;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        // //
        $count = User::count();

       $chart = Charts::database(User::all(),'line','highcharts')
                ->title('User Statistics')
                ->groupByYear(7)
                ->Width(0)
                ->ElementLabel('Total Users Registered')
                ->Responsive(true);



        return view('dashboard',['chart'=>$chart])->with('count', $count);
        // return view('dashboard', ['chart' => $chart]);


        // $count = User::count();
        // return View::make('dashboard')->with('count', $count);

        // return view('dashboard');
    }

    public function logout () {
        //logout user
        auth()->logout();
        // redirect to homepage
        return redirect('/coindeoro');
    }


    //FETCH DATA FROM THE SECOND DATABASE
    public function another(){
        // $arr_graph = \DB::connection('mysql2')->select("SELECT * FROM ico_stages WHERE id = ?", [1]);

        $arr_graph = \DB::connection('mysql2')->select("SELECT * FROM ico_stages WHERE id = ?", [1]);

        //DISPLAY
        dd($arr_graph);


    }
}

我的 web.php

//dashboard
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
//FETCH DATA FROM SECOND DB
Route::get('/another', 'HomeController@another')->name('another');
//for user logout
Route::get('/logout', 'HomeController@logout');
```

My **.env** file

```
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HoQcNyCc5KEGw4yjqpBIdKzTC+yeDoOJcerVMEVx+fs=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=adminpanel
DB_USERNAME=root
DB_PASSWORD=



DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=ricjac8_orocoin
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
```

2 个答案:

答案 0 :(得分:0)

让我们在config / database.php中创建一个新的数据库连接:

'connections' => [
    'mysql' => [
        // ....
    ],
    'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST_SECOND', '127.0.0.1'),
        'port' => env('DB_PORT_SECOND', '3306'),
        'database' => env('DB_DATABASE_SECOND', 'forge'),
        'username' => env('DB_USERNAME_SECOND', 'forge'),
        'password' => env('DB_PASSWORD_SECOND', ''),
        // The followings are default Laravel settings, which could be redundant.
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ]
]

在控制器中,您可以这样说:

$chart = Charts::database(User::on('mysql2')->all(),'line','highcharts')
    ->title('User Statistics')
    ->groupByYear(7)
    ->Width(0)
    ->ElementLabel('Total Users Registered')
    ->Responsive(true);

return view('dashboard',['chart'=>$chart])->with('count', $count);

答案 1 :(得分:0)

@Kevin Bui的最新答案

'connections' => [
    'mysql' => [
        // ....
    ],
    'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST_SECOND', '127.0.0.1'),
        'port' => env('DB_PORT_SECOND', '3306'),
        'database' => env('DB_DATABASE_SECOND', 'forge'),
        'username' => env('DB_USERNAME_SECOND', 'forge'),
        'password' => env('DB_PASSWORD_SECOND', ''),
        // The followings are default Laravel settings, which could be redundant.
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ]
]
In the controller, you can say something like:

$chart = Charts::database(User::on('mysql2')->all(),'line','highcharts')
    ->title('User Statistics')
    ->groupByYear(7)
    ->Width(0)
    ->ElementLabel('Total Users Registered')
    ->Responsive(true);

return view('dashboard',compact('chart','count'));;