A Laravel package to integrate InfluxDB 2.0 using the official influxdb-client-php library.
Add the package to your composer.json
file:
"require": {
"rikodev/laravel-influxdb2": "dev-master"
},
"repositories": [
{
"url": "https://github.com/RikoDEV/laravel-influxdb2.git",
"type": "git"
}
]
Then run:
composer update rikodev/laravel-influxdb2
Note: Laravel 5.5+ supports automatic package discovery. You can skip this step.
For older versions, add the provider and alias to your config/app.php
:
'providers' => [
RikoDEV\InfluxDB\Providers\ServiceProvider::class,
],
'aliases' => [
'InfluxDB' => RikoDEV\InfluxDB\Facades\InfluxDB::class,
],
Add the following environment variables to your .env
file:
INFLUXDB_HOST=localhost
INFLUXDB_PORT=8086
INFLUXDB_TOKEN=
INFLUXDB_BUCKET=
INFLUXDB_ORG=
Run the following command to publish the configuration file:
php artisan vendor:publish --provider="RikoDEV\InfluxDB\Providers\ServiceProvider"
use RikoDEV\InfluxDB\Facades\InfluxDB;
// Get the query client
$queryApi = InfluxDB::createQueryApi();
// Query data
$result = $queryApi->queryRaw(
"from(bucket: \"my-bucket\")
|> range(start: 0)
|> filter(fn: (r) => r[\"_measurement\"] == \"weather\"
and r[\"_field\"] == \"temperature\"
and r[\"location\"] == \"Sydney\")"
);
InfluxDB::close();
use RikoDEV\InfluxDB\Facades\InfluxDB;
use InfluxDB2\Point;
// Get the write client
$writeApi = InfluxDB::createWriteApi();
// Create an array of points and write them to InfluxDB
$result = $writeApi->write([
Point::measurement("blog_posts")
->addTag("post_id", $post->id)
->addField("likes", 6)
->addField("comments", 3)
->time(time())
]);
InfluxDB::close();
This project is licensed under the MIT License. Based on the tray-labs/laravel-influxdb project.
Feel free to open an issue or submit a PR if you’d like to improve the project!
🚀 Happy coding!