NexaUI framework menyediakan sistem query database yang kuat dan fleksibel melalui NexaModel.
NexaModel secara otomatis terhubung ke database menggunakan konfigurasi yang ditentukan dalam file lingkungan Anda.
# Database Configuration
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=nexaui
DB_USERNAME=root
DB_PASSWORD=secret
Koneksi database ditangani oleh kelas NexaDb
yang menggunakan PDO untuk keamanan dan kompatibilitas.
NexaModel menyediakan berbagai metode untuk melakukan query database:
// Mengambil semua data dari tabel 'users'
$users = $this->Storage('users')->get();
// Dengan kolom tertentu
$users = $this->Storage('users')->select('id, username, email')->get();
// Mengambil baris pertama
$user = $this->Storage('users')->first();
// Mengambil baris terakhir (berdasarkan id)
$user = $this->Storage('users')->last();
// Mengambil baris berdasarkan ID
$user = $this->Storage('users')->where('id', 1)->first();
// Menghitung jumlah baris
$count = $this->Storage('users')->count();
// Menghitung jumlah dengan kondisi
$activeCount = $this->Storage('users')->where('status', 'active')->count();
// Sum, Average, Min, Max
$totalBalance = $this->Storage('accounts')->sum('balance');
$avgBalance = $this->Storage('accounts')->avg('balance');
$minBalance = $this->Storage('accounts')->min('balance');
$maxBalance = $this->Storage('accounts')->max('balance');
NexaModel menyediakan berbagai metode untuk menambahkan kondisi ke query Anda:
// Kondisi WHERE sederhana
$users = $this->Storage('users')
->where('status', 'active')
->get();
// Dengan operator perbandingan
$users = $this->Storage('users')
->where('age', '>=', 18)
->get();
// Multiple WHERE (AND)
$users = $this->Storage('users')
->where('status', 'active')
->where('age', '>=', 18)
->get();
// WHERE dengan array (semua kondisi AND)
$users = $this->Storage('users')
->where([
'status' => 'active',
'role' => 'admin'
])
->get();
// OR WHERE
$users = $this->Storage('users')
->where('status', 'active')
->orWhere('status', 'pending')
->get();
// WHERE IN
$users = $this->Storage('users')
->whereIn('role', ['admin', 'editor'])
->get();
// WHERE NOT IN
$users = $this->Storage('users')
->whereNotIn('status', ['banned', 'deleted'])
->get();
// OR WHERE IN
$users = $this->Storage('users')
->where('status', 'active')
->orWhereIn('role', ['admin', 'editor'])
->get();
// WHERE IS NULL
$users = $this->Storage('users')
->whereNull('deleted_at')
->get();
// WHERE IS NOT NULL
$users = $this->Storage('users')
->whereNotNull('email_verified_at')
->get();
// WHERE BETWEEN
$users = $this->Storage('users')
->whereBetween('age', [18, 30])
->get();
// WHERE NOT BETWEEN
$users = $this->Storage('users')
->whereNotBetween('created_at', ['2023-01-01', '2023-01-31'])
->get();
// WHERE DATE
$users = $this->Storage('users')
->whereDate('created_at', '=', '2023-06-15')
->get();
// WHERE YEAR
$users = $this->Storage('users')
->whereYear('created_at', '=', '2023')
->get();
// WHERE MONTH
$users = $this->Storage('users')
->whereMonth('created_at', '=', '6')
->get();
// WHERE DAY
$users = $this->Storage('users')
->whereDay('created_at', '=', '15')
->get();
// WHERE LIKE
$users = $this->Storage('users')
->where('name', 'LIKE', '%John%')
->get();
// Shorthand LIKE (otomatis menambahkan %)
$users = $this->Storage('users')
->where('name', 'LIKE', 'John')
->get(); // Sama dengan '%John%'
// ORDER BY sederhana
$users = $this->Storage('users')
->orderBy('created_at', 'DESC')
->get();
// Multiple ORDER BY
$users = $this->Storage('users')
->orderBy('role')
->orderBy('created_at', 'DESC')
->get();
// ORDER BY dengan array
$users = $this->Storage('users')
->orderBy(['role', 'created_at'], 'DESC')
->get();
// Random ordering
$users = $this->Storage('users')
->inRandomOrder()
->get();
// LIMIT
$users = $this->Storage('users')
->limit(10)
->get();
// LIMIT dengan OFFSET
$users = $this->Storage('users')
->limit(10)
->offset(20)
->get();
// Alias untuk limit dan offset
$users = $this->Storage('users')
->take(10) // Alias untuk limit()
->skip(20) // Alias untuk offset()
->get();
NexaModel mendukung berbagai jenis JOIN untuk menghubungkan tabel:
$users = $this->Storage('users')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*, orders.total')
->get();
$users = $this->Storage('users')
->leftJoin('orders', 'users.id', '=', 'orders.user_id')
->select('users.*, orders.total')
->get();
$usersByRole = $this->Storage('users')
->select('role, COUNT(*) as user_count')
->groupBy('role')
->get();
$popularRoles = $this->Storage('users')
->select('role, COUNT(*) as user_count')
->groupBy('role')
->having('user_count', '>', 5)
->get();
Untuk kasus yang memerlukan query SQL kustom:
// Raw SQL query
$results = $this->Storage('users')->raw('SELECT * FROM users WHERE status = ?', ['active']);
// Debugging query
$this->Storage('users')
->where('status', 'active')
->dd(); // Menampilkan SQL dan binding values
// Dump query tanpa menghentikan eksekusi
$this->Storage('users')
->where('status', 'active')
->dump()
->get();
NexaModel menyediakan metode untuk pagination yang mudah digunakan:
// Pagination dasar
$page = $this->getParam('page', 1);
$perPage = 15;
$results = $this->Storage('users')
->where('status', 'active')
->paginate($page, $perPage);
// $results berisi:
// - data: array hasil query
// - total: total records
// - last_page: nomor halaman terakhir
// - current_page: nomor halaman saat ini
// - per_page: jumlah item per halaman
// Hanya info pagination tanpa data
$paginationInfo = $this->Storage('users')
->where('status', 'active')
->paginateInfo($page, $perPage);
// DISTINCT values
$roles = $this->Storage('users')
->distinct('role')
->get();
// DISTINCT dengan multiple columns
$userStatuses = $this->Storage('users')
->distinct(['role', 'status'])
->get();
// Membuat query pertama
$activeUsers = $this->Storage('users')->where('status', 'active');
// Membuat query kedua dan gabungkan dengan UNION
$results = $this->Storage('archived_users')
->where('status', 'active')
->union($activeUsers)
->get();
// UNION ALL (termasuk duplikat)
$results = $this->Storage('archived_users')
->where('status', 'active')
->unionAll($activeUsers)
->get();