Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TblQuotationsBuilder
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 addUpdatedAtColumn
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace App\Models\Builders;
4
5use Illuminate\Database\Eloquent\Builder;
6
7/**
8 * Stamps tbl_quotations.updated_at on every query-builder write
9 * (`TblQuotations::where(...)->update()`, `->increment()`, `->decrement()`).
10 *
11 * The DWH loads quotations whose updated_at changed, so any write that skips
12 * it is invisible there. TblQuotations keeps `$timestamps = false` (turning it
13 * on would also cast/serialise updated_at as a Carbon ISO string in every API
14 * response), so Eloquent's own updated_at handling is off — this does the one
15 * part we want. An explicit `updated_at` in the payload wins (e.g. a caller
16 * that deliberately preserves or copies a value).
17 *
18 * Writes that bypass Eloquent (`DB::table('tbl_quotations')`, raw SQL) must
19 * set updated_at themselves.
20 */
21class TblQuotationsBuilder extends Builder
22{
23    protected function addUpdatedAtColumn(array $values)
24    {
25        $segments = preg_split('/\s+as\s+/i', (string) $this->query->from);
26        $qualified = end($segments).'.updated_at';
27
28        if (array_key_exists('updated_at', $values) || array_key_exists($qualified, $values)) {
29            return $values;
30        }
31
32        return array_merge(['updated_at' => date('Y-m-d H:i:s')], $values);
33    }
34}