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