summaryrefslogtreecommitdiff
path: root/src/Models/BaseModel.php
blob: 49255905720ace38a1bb274939fe78000a870d69 (plain)
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
42
43
44
45
46
47
48
49
50
51
<?php

namespace Engelsystem\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    /** @var bool Disable timestamps by default because of "Datensparsamkeit" */
    public $timestamps = false;

    /**
     * Create a new model
     *
     * @param array $attributes
     * @return BaseModel
     */
    public function create(array $attributes = [])
    {
        $instance = new static($attributes);
        $instance->save();

        return $instance;
    }

    /**
     * Find a model by its primary key
     *
     * @param mixed $id
     * @param array $columns
     * @return Builder|Builder[]|Collection|static|null
     */
    public static function find($id, $columns = ['*'])
    {
        return static::query()->find($id, $columns);
    }

    /**
     * Find a model by its attributes or create a new one
     *
     * @param mixed $id
     * @param array $columns
     * @return static|Model
     */
    public static function findOrNew($id, $columns = ['*'])
    {
        return static::query()->findOrNew($id, $columns);
    }
}