WHMCS documentation about database management is very essential and incomplete, lacking any real sample code.
WHMCS use Laravel framework's database component, and this is great if you yet know Laravel, otherwise realizing even a simple operation may be hard.
We'll give here few different samples of code to do various operations:
Get the domain name of the service id# 5 from table tblhosting
use Illuminate\Database\Capsule\Manager as Capsule;
$serviceid=5 ;
try {
$servicename = Capsule::table('tblhosting')
->where("id", $serviceid)
->select('domain')
->first();
echo "<pre>"; print_r($servicename);
} catch(\Illuminate\Database\QueryException $ex){
echo $ex->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
$domainname=$servicename->domain ;
Edit the description of a to-do element in tbltodolist
use Illuminate\Database\Capsule\Manager as Capsule;
$newdescription = 'this is the new text for the to-do item n# 37';
$todoitemid = 37 ;
try {
$update_data = [
'description' => $newdescription
];
Capsule::table('tbltodolist')
->where('id', '=', $todoitemid)
->update($update_data);
} catch(\Illuminate\Database\QueryException $ex){
echo $ex->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}