|
1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Support\Bases\Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class AbstractModel |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelTracker\Models |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property int id |
|
12
|
|
|
* |
|
13
|
|
|
* @method static \Arcanedev\LaravelTracker\Models\AbstractModel firstOrCreate(array $attributes, array $values = []) |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractModel extends Model |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Constructor |
|
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
*/ |
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new Eloquent model instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $attributes |
|
25
|
|
|
*/ |
|
26
|
22 |
|
public function __construct(array $attributes = []) |
|
27
|
|
|
{ |
|
28
|
22 |
|
$this->setConnection($this->getConfig('database.connection', null)) |
|
29
|
22 |
|
->setPrefix($this->getConfig('database.prefix', 'tracker_')); |
|
30
|
|
|
|
|
31
|
22 |
|
parent::__construct($attributes); |
|
32
|
22 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
35
|
|
|
| Other Functions |
|
36
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
37
|
|
|
*/ |
|
38
|
|
|
/** |
|
39
|
|
|
* Get the tracker config. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $key |
|
42
|
|
|
* @param mixed|null $default |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
22 |
|
protected function getConfig($key, $default = null) |
|
47
|
|
|
{ |
|
48
|
22 |
|
return config("laravel-tracker.$key", $default); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
52
|
|
|
| Laravel 5.2 & 5.1 Support |
|
53
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
54
|
|
|
*/ |
|
55
|
|
|
/** |
|
56
|
|
|
* Handle dynamic method calls into the model. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $method |
|
59
|
|
|
* @param array $parameters |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
20 |
|
public function __call($method, $parameters) |
|
64
|
|
|
{ |
|
65
|
20 |
|
if (version_compare(app()->version(), '5.3.0', '<') && in_array($method, ['firstOrCreate'])) |
|
66
|
10 |
|
return call_user_func_array([$this, 'custom'.ucfirst($method)], $parameters); |
|
67
|
|
|
|
|
68
|
20 |
|
return parent::__call($method, $parameters); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the first record matching the attributes or create it. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $attributes |
|
75
|
|
|
* @param array $values |
|
76
|
|
|
* |
|
77
|
|
|
* @return self |
|
78
|
|
|
*/ |
|
79
|
|
|
public function customFirstOrCreate(array $attributes, array $values = []) |
|
80
|
|
|
{ |
|
81
|
|
|
$instance = $this->newInstance(); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($attributes as $key => $value) { |
|
84
|
|
|
$instance = $instance->where($key, $value); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ( ! is_null($first = $instance->first())) return $first; |
|
88
|
|
|
|
|
89
|
|
|
$instance = $this->newInstance($attributes + $values); |
|
90
|
|
|
$instance->save(); |
|
91
|
|
|
|
|
92
|
|
|
return $instance; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|