Skip to content

Create Group

Create a new group on your Traccar server.

IMPORTANT

  • The name field is required
  • The id field is ignored when creating; Traccar will generate a new ID
  • Use groupId to create hierarchical (nested) groups

Usage

php
use ThingsTelemetry\Traccar\Dto\GroupData;
use ThingsTelemetry\Traccar\Facades\Group;

// Create a top-level group
$groupData = new GroupData(
    name: 'Vehicles',
    attributes: [
        'color' => 'blue',
        'icon' => 'car',
    ],
);

$group = Group::create($groupData); // returns ThingsTelemetry\Traccar\Dto\GroupData

// Create a child group (nested under another group)
$childGroupData = new GroupData(
    name: 'Trucks',
    groupId: 1, // Parent group ID
    attributes: [
        'color' => 'green',
    ],
);

$childGroup = Group::create($childGroupData);

Results

The response is a ThingsTelemetry\Traccar\Dto\GroupData instance with the created group's data.

php
$group->id;          // 2 (generated by server)
$group->name;        // "Vehicles"
$group->groupId;     // null (top-level)
$group->attributes;  // ['color' => 'blue', 'icon' => 'car']

Attributes

The attributes array allows you to store custom metadata for groups:

php
$attributes = [
    'color' => 'blue',
    'icon' => 'truck',
    'description' => 'Company vehicle fleet',
    'location' => 'Nairobi Depot',
];

$groupData = new GroupData(
    name: 'Fleet A',
    attributes: $attributes,
);

Hierarchical Groups

Groups can be nested to create a hierarchy:

php
// Parent group
$fleet = Group::create(new GroupData(name: 'Fleet'));
$vehicles = Group::create(new GroupData(name: 'Vehicles', groupId: $fleet->id));
$cars = Group::create(new GroupData(name: 'Cars', groupId: $vehicles->id));

Errors

400 - Group name already exists

The group name may need to be unique within the same parent group.

400 - Parent group not found

shell
Bad Request (400) Response: Key (groupid)=(9999) is not present in table "tc_groups".

This means the specified groupId (parent) does not exist. Use the groups API to get valid group IDs.

Released under the MIT License.