Introduction
The InfluxDB Integration for Laravel, created by IanKibet, provides an easy-to-use package for interacting with InfluxDB within your Laravel application. This guide covers the installation, configuration, and usage of the package to write and query data from InfluxDB.
Installation
To install the package, follow these steps:
Install via Composer:
composer require iankibet/influxdb
Publish the Configuration File:
php artisan vendor:publish --provider="Iankibet\InfluxDB\InfluxDBServiceProvider"
Configure Environment Variables: Set the following variables in your .env
file:
INFLUXDB_HOST=127.0.0.1
INFLUXDB_PORT=8086
INFLUXDB_TOKEN=
INFLUXDB_BUCKET=
INFLUXDB_ORG=
Usage
Writing Data
To write data to InfluxDB, you need to use the InfluxDbPoint
class and the InfluxDb
facade. Here's an example of how to write data:
use Iankibet\InfluxDb\InfluxDbPoint;
use Iankibet\InfluxDb\Facades\InfluxDb;
// In your controller or method
$point = new InfluxDbPoint();
$point->setMeasurement('measurement_name');
$point->setTags(['tag_key' => 'tag_value']);
$point->setFields(['field_key' => 'field_value']);
$point->setTime(time());
InfluxDb::write($point);
Querying Data
To query data from InfluxDB, use the InfluxDb
facade. Here’s an example:
use Iankibet\InfluxDb\Facades\InfluxDb;
$measurement = 'measurement_name';
$fields = [
'key1' => 'value1',
'key2' => 'value2'
];
$from = '2021-01-01T00:00:00Z';
$to = '2021-01-02T00:00:00Z';
$res = InfluxDb::query($measurement, $fields, $from, $to);
Conclusion
By following this guide, you can easily integrate InfluxDB with your Laravel application, enabling you to efficiently write and query time-series data. This package simplifies the process, allowing you to focus on building robust and data-driven applications.
For more details, visit the InfluxDB Integration for Laravel GitHub page.