PHP code example of istiyakamin / laradantic

1. Go to this page and download the library: Download istiyakamin/laradantic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

istiyakamin / laradantic example snippets


use LaraDantic\Schema\Schema;

class FlightBooking extends Schema
{
    public string $origin;

    public string $destination;

    public string $departureDate;

    public ?string $returnDate;

    public int $passengers = 1;

    public Cabin $cabin;
}

$booking = FlightBooking::from([
    'origin' => 'VIE',
    'destination' => 'DAC',
    'departureDate' => '2026-10-15',
    'returnDate' => null,
    'passengers' => 2,
    'cabin' => 'business',
]);

$booking->cabin; // Cabin::Business

$booking->toArray();

use LaraDantic\Schema\Attributes\Description;
use LaraDantic\Schema\Attributes\Example;
use LaraDantic\Schema\Attributes\Max;
use LaraDantic\Schema\Attributes\Min;

class FlightBooking extends Schema
{
    #[Description('Departure airport IATA code')]
    #[Example('VIE')]
    public string $origin;

    #[Min(1)]
    #[Max(20)]
    public int $passengers = 1;

    // ...
}

FlightBooking::jsonSchema();
FlightBooking::jsonSchemaJson(); // pretty-printed JSON string

$result = FlightBooking::validate($data);

if ($result->fails()) {
    $result->errors(); // ['passengers' => ['The passengers field must not be greater than 20.']]
}

// Or validate-then-hydrate in one call, throwing on failure:
$booking = FlightBooking::validated($data); // throws SchemaValidationException

$booking = AI::structured(FlightBooking::class)
    ->system('You are a travel booking assistant.')
    ->prompt('Fly VIE to DAC on 2026-10-15 for 2 people, business class.')
    ->run(); // FlightBooking instance, validated and hydrated

use LaraDantic\Tools\Tool;
use LaraDantic\Tools\ToolResult;

class CreateFlightBooking extends Tool
{
    public function name(): string
    {
        return 'create_flight_booking';
    }

    public function description(): string
    {
        return 'Create a flight booking request.';
    }

    public function schema(): string
    {
        return FlightBooking::class;
    }

    public function execute(FlightBooking $booking): ToolResult
    {
        // Application implementation - create the booking, return a result.
        return ToolResult::success(['id' => 'BK-123']);
    }
}

$toolCalls = AI::tools([CreateFlightBooking::class, SearchFlight::class])
    ->message('I want to book a flight from Vienna to Dhaka.')
    ->run(); // Collection<ToolCall> - empty if the model replied with plain text instead

$toolCall = $toolCalls->first();

if ($toolCall->

$result = AI::agent()
    ->tools([SearchFlight::class, CreateFlightBooking::class])
    ->maxIterations(5)
    ->system('You are a travel booking assistant.')
    ->run('I want to book a flight from Vienna to Dhaka for 2 people.');

match ($result->status()) {
    AgentStatus::Completed => $result->text(),
    AgentStatus::PendingConfirmation => /* ask the user, then see below */,
    AgentStatus::MaxIterationsReached => /* give up gracefully */,
};

$toolCall = $result->pendingToolCalls()[0];
$toolResult = $toolCall->execute(); // explicit, after confirmation

$resumed = AI::agent()->tools([SearchFlight::class, CreateFlightBooking::class])
    ->resume($result->messages(), [['toolCall' => $toolCall, 'result' => $toolResult]]);