File size: 960 Bytes
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace App\Infrastructure\Database;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

abstract class BaseRepository
{
    abstract protected function table(): string;

    protected function query(bool $includeDeleted = false): Builder
    {
        $query = DB::table($this->table());

        if (! $includeDeleted && $this->hasDeletedAtColumn()) {
            $query->whereNull('deleted_at');
        }

        return $query;
    }

    protected function insert(array $attributes): bool
    {
        $now = Carbon::now();

        return DB::table($this->table())->insert(array_merge([
            'created_at' => $now,
            'updated_at' => $now,
        ], $attributes));
    }

    protected function hasDeletedAtColumn(): bool
    {
        static $cache = [];

        return $cache[$this->table()] ??= DB::getSchemaBuilder()->hasColumn($this->table(), 'deleted_at');
    }
}