PHP code example of codyjheiser / laravel-db2-eloquent
1. Go to this page and download the library: Download codyjheiser/laravel-db2-eloquent 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/ */
codyjheiser / laravel-db2-eloquent example snippets
// Correct - testing() before withExtensions()
Customer::testing()->withExtensions()->first();
// WRONG - extension tables will still use production schema
Customer::withExtensions()->testing()->first();
// These are equivalent - selectMapped is applied automatically
Customer::get();
Customer::selectMapped()->get();
// Also applies to eager-loaded relations automatically
Customer::with('orders')->get();
// Bypass auto-select to get all columns
Customer::selectAll()->get();
// Or use explicit select()
Customer::select('*')->get();
Customer::select(['RMCUST', 'RMNAME', 'SOME_UNMAPPED_COL'])->get();
protected bool $autoSelectMapped = false;
protected bool $applyMapsOnOutput = false;
class CommissionPricing extends Model
{
protected array $maps = [
'PBCMP' => 'company_number',
'PBPSLS' => 'pricing_sales',
];
// Either style works
protected $casts = [
'PBCMP' => 'integer', // Raw DB column name
'pricing_sales' => 'decimal:2', // Mapped name
];
}
use CodyJHeiser\Db2Eloquent\Casts\IbmDateTime;
protected $casts = [
// Single datetime column (e.g., 20251217143052)
'SHDTTM' => IbmDateTime::class,
// Two columns: date (Ymd) + time (Hms)
'SHDATE,SHTIME' => IbmDateTime::class,
// With mapped column names
'scheduled_date,scheduled_time' => IbmDateTime::class,
// With output format
'SHDATE,SHTIME' => IbmDateTime::class.':datetime', // "2025-12-17 14:30:52"
'SHDATE,SHTIME' => IbmDateTime::class.':Y-m-d H:i:s', // "2025-12-17 14:30:52"
'SHDATE,SHTIME' => IbmDateTime::class.':us', // "12/17/2025 2:30:52 PM"
];
// Access via the first column name (or its mapped name)
$schedule->SHDATE; // Carbon instance with date + time combined
$schedule->scheduled_date; // Same Carbon instance
// Setting updates both columns
$schedule->scheduled_date = Carbon::now();
$schedule->scheduled_date = '2025-12-17 14:30:52';
// toArray()
protected array $maps = [
'RMCUST' => 'customer_number', // This one wins
'CXCUST' => 'customer_number', // Skipped in output
];
// Include inactive/deleted records
Item::withInactive()->get();
// Include all companies
Item::withAllCompanies()->get();
// Filter by a specific company
Item::forCompany('2')->get();
// Remove all automatic filters
Item::unfiltered()->get();
// Combine bypasses
Item::withInactive()->withAllCompanies()->get();
class SomeModel extends Model
{
// Disable automatic filtering
protected bool $filterActiveOnly = false;
protected bool $filterByCompany = false;
// Or change the default values
protected string $activeDeleteCode = 'I'; // Different "active" code
protected string $defaultCompany = '2'; // Different default company
}
class ItemBalance extends Model
{
protected array $maps = [
'IFCOMP' => 'company_number',
'IFITEM' => 'item_number',
];
public function item()
{
// If both models map to 'company_number' and 'item_number'
return $this->belongsTo(Item::class, ['company_number', 'item_number']);
// Single column works too
// return $this->belongsTo(Item::class, 'item_number');
// Or specify both sides explicitly
// return $this->belongsTo(Item::class, ['company_number', 'item_number'], ['company_number', 'item_number']);
// Raw DB columns still work
// return $this->belongsTo(Item::class, ['IFCOMP', 'IFITEM'], ['ICCMP', 'ICITEM']);
}
}
class Item extends Model
{
public function balances()
{
return $this->hasMany(ItemBalance::class, ['company_number', 'item_number']);
}
public function primaryBalance()
{
return $this->hasOne(ItemBalance::class, ['company_number', 'item_number']);
}
}
use CodyJHeiser\Db2Eloquent\Model;
use CodyJHeiser\Db2Eloquent\Concerns\HasUnionSources;
class ServiceCallAll extends Model
{
use HasUnionSources;
protected $connection = 'vai';
protected string $schema = 'R60FILES';
protected $table = 'SBSCHD'; // Fallback for connection/schema resolution
protected array $unionSources = [
ServiceCallLive::class,
ServiceCallHistory::class,
];
// Only shared columns — one table may have extra columns you don't need here
protected array $maps = [
'SHCMP' => 'company_number',
'SHCUST' => 'customer_number',
'SHCLNO' => 'call_number',
'SHCLST' => 'status',
'SHCLDT' => 'call_date',
'SHCITY' => 'city',
'SHSTAT' => 'state',
];
public function customer()
{
return $this->belongsTo(Customer::class, ['customer_number']);
}
}
class ServiceCallLive extends ServiceCallAll
{
protected $table = 'SBSCHD';
// Optionally override $maps to add columns specific to this table
// Optionally override $casts for table-specific cast behavior
}
class ServiceCallHistory extends ServiceCallAll
{
protected $table = 'SBHSHD';
}
// Returns results from BOTH tables
ServiceCallAll::where('status', 'Q')->limit(10)->get();
ServiceCallAll::where('call_number', 12345)->first();
ServiceCallAll::exists();
// Auto-filtering works — applied inside each source query
ServiceCallAll::get(); // Active records, company 1, from both tables
ServiceCallAll::unfiltered()->get(); // All records from both tables
ServiceCallAll::withInactive()->get(); // Include deleted records from both tables
// Log to stderr (default)
Item::logQuery()->where('item_number', 'ABC')->first();
// Log to file
Item::logQuery('default')->selectMapped()->get();
// Log to both stderr and file
Item::logQuery(['stderr', 'default'])->withExtensions()->first();
use CodyJHeiser\Db2Eloquent\Model;
// Enable via base model or any child model
Model::enableQueryLog();
Customer::enableQueryLog();
// Log to app log file instead of stderr
Customer::enableQueryLog(null);
Customer::enableQueryLog('default');
// Log to multiple channels
Customer::enableQueryLog(['stderr', 'default']);
// Run queries...
Customer::where('customer_number', '123')->first();
// Manage logs
Customer::dumpQueryLog(); // Dump to output
Customer::getQueryLog(); // Get as array
Customer::clearQueryLog(); // Clear log
Customer::disableQueryLog(); // Turn off