<?php
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

$search = "a"; // test with a general character
$count = App\Models\Peminjaman::where(function($q) use ($search) {
    $q->whereHas('user', function($qUser) use ($search) {
        $qUser->where('name', 'like', "%{$search}%");
    })->orWhereHas('buku', function($qBuku) use ($search) {
        $qBuku->where('judul', 'like', "%{$search}%");
    });
})->count();

echo "Count with 'a': " . $count . PHP_EOL;

$all = App\Models\Peminjaman::count();
echo "Total Peminjaman: " . $all . PHP_EOL;

$first = App\Models\Peminjaman::with(['user', 'buku'])->first();
if ($first) {
    echo "First Peminjaman User Name: " . ($first->user ? $first->user->name : 'N/A') . PHP_EOL;
    echo "First Peminjaman Buku Judul: " . ($first->buku ? $first->buku->judul : 'N/A') . PHP_EOL;
}
