Skip to content

Laravel Usage

Once installed, BioTime\BioTime is available from the container like any other singleton service. There is no facade or helper function shipped by the package — resolve it via the container or constructor injection.

Resolving from the container

php
use BioTime\BioTime;

$biotime = app(BioTime::class);

$employees = $biotime->employees()->all();

Example controller

php
namespace App\Http\Controllers;

use BioTime\BioTime;
use BioTime\Exceptions\ApiException;
use Illuminate\Http\JsonResponse;

class EmployeeController extends Controller
{
    public function __construct(private readonly BioTime $biotime)
    {
    }

    public function index(): JsonResponse
    {
        try {
            $employees = $this->biotime->employees()->all(
                filters: ['ordering' => 'first_name'],
            );
        } catch (ApiException $e) {
            return response()->json(['error' => $e->getMessage()], 502);
        }

        return response()->json($employees);
    }
}

Example service class

php
namespace App\Services;

use BioTime\BioTime;
use BioTime\DTO\AttendanceRecord;

class AttendanceSyncService
{
    public function __construct(private readonly BioTime $biotime)
    {
    }

    /**
     * @return AttendanceRecord[]
     */
    public function fetchRange(string $from, string $to): array
    {
        return $this->biotime->attendances()->all($from, $to);
    }
}

Example Artisan command

php
namespace App\Console\Commands;

use App\Services\AttendanceSyncService;
use Illuminate\Console\Command;

class SyncAttendance extends Command
{
    protected $signature = 'biotime:sync-attendance {from} {to}';
    protected $description = 'Pull attendance records from BioTime for a date range';

    public function handle(AttendanceSyncService $sync): int
    {
        $records = $sync->fetchRange($this->argument('from'), $this->argument('to'));

        $this->info(sprintf('Fetched %d attendance records.', count($records)));

        foreach ($records as $record) {
            // Persist to your own database, dispatch events, etc.
        }

        return self::SUCCESS;
    }
}

Schedule it in your Laravel scheduler (e.g. routes/console.php or App\Console\Kernel) if you want a recurring sync — see Examples for a scheduled-sync walkthrough.

Token caching in Laravel

The service provider automatically injects Laravel's default cache store ($this->app['cache.store']) into Config, so the device token persists across requests without any extra code — see Caching.

Distributed under the MIT License.