<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;

$capsule = new Capsule; $capsule->addConnection(['driver'=>'sqlite','database'=>':memory:','prefix'=>'']);
$capsule->setAsGlobal(); $capsule->bootEloquent();
Capsule::schema()->create('demo_orders', function(Blueprint $table) { $table->id(); $table->string('name'); });
Capsule::schema()->create('demo_lines', function(Blueprint $table) { $table->id(); $table->unsignedBigInteger('order_id'); $table->integer('quantity'); });
class DemoOrder extends Model { protected $table='demo_orders'; public $timestamps=false; protected $guarded=[]; public function lines(){return $this->hasMany(DemoLine::class,'order_id');} }
class DemoLine extends Model { protected $table='demo_lines'; public $timestamps=false; protected $guarded=[]; }
Capsule::connection()->transaction(function(){ for($i=1;$i<=4;$i++){ $order=DemoOrder::create(['name'=>'Synthetic order '.$i]); $order->lines()->create(['quantity'=>1]); } });
$connection=Capsule::connection();$connection->enableQueryLog();$connection->flushQueryLog();
$lazy=[];foreach(DemoOrder::all() as $order){$lazy[]=$order->lines->sum('quantity');}
$lazyQueries=count($connection->getQueryLog());$connection->flushQueryLog();
$eager=[];foreach(DemoOrder::with('lines')->get()as $order){$eager[]=$order->lines->sum('quantity');}
$eagerQueries=count($connection->getQueryLog());
if($lazy!==$eager || $lazyQueries!==5 || $eagerQueries!==2){throw new RuntimeException('Eloquent query comparison failed.');}
echo json_encode(['runtime'=>'Laravel Illuminate Database 13.38.0 on SQLite','orders'=>4,'lazyQueries'=>$lazyQueries,'eagerQueries'=>$eagerQueries,'sameResult'=>$lazy===$eager], JSON_PRETTY_PRINT),PHP_EOL;
