|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use App\Repositories\Contracts\RepositoryInterface; |
|
7
|
|
|
use App\Repositories\Exceptions\RepositoryException; |
|
8
|
|
|
use Illuminate\Container\Container as Application; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
|
|
11
|
|
|
abstract class BaseRepository implements RepositoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Application |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $app; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Model |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $model; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Application $app |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Application $app) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->app = $app; |
|
28
|
|
|
$this->makeModel(); |
|
29
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return Model |
|
34
|
|
|
* @throws RepositoryException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function makeModel() |
|
37
|
|
|
{ |
|
38
|
|
|
$model = $this->app->make($this->model()); |
|
39
|
|
|
if (!$model instanceof Model) { |
|
40
|
|
|
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"); |
|
41
|
|
|
} |
|
42
|
|
|
return $this->model = $model; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Specify Model class name |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract public function model(); |
|
51
|
|
|
|
|
52
|
|
|
public function all($columns = array('*')) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->model->get($columns); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function create(array $columns) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->model->create($columns); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function findByField($field = null, $value = null, $columns = ['*']) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->model->where($field, '=', $value)->get($columns); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function find($value = null, $columns = ['*']) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->model->where('id', '=', $value)->first($columns); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve data array for populate field select |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $column |
|
78
|
|
|
* @param string|null $key |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Illuminate\Support\Collection|array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function pluck($column, $key = null) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->model->pluck($column, $key); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Load relations |
|
90
|
|
|
* |
|
91
|
|
|
* @param array|string $relations |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function with($relations) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->model = $this->model->with($relations); |
|
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
public function orderBy($column, $direction = 'asc') |
|
103
|
|
|
{ |
|
104
|
|
|
$this->model = $this->model->orderBy($column, $direction); |
|
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..