Skip to content

Commit 11146e8

Browse files
author
Yoel Monzon
committed
[add] new functions and fix docs
1 parent 72fc492 commit 11146e8

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# IntelliJ project files
3+
.idea
4+
*.iml
5+
out
6+
gen
7+
# Created by .ignore support plugin (hsz.mobi)

src/Repository.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class Repository implements RepositoryInterface
3232
function __construct(Application $app) {
3333
$this->app = $app;
3434
$this->makeModel();
35-
$this->relations = $this->model->_relations;
35+
$this->relations = ($this->model->_relations ? $this->model->_relations : [] );
3636
}
3737

3838
/**
@@ -44,15 +44,15 @@ abstract function model();
4444
/**
4545
* Function to create the instance of Eloquent Model
4646
* @return Model
47-
* @throws Exception
47+
* @throws \Exception
4848
*/
4949
public function makeModel()
5050
{
5151
$name_model = $this->model();
5252
$model = $this->app->make($name_model);
5353

5454
if(!$model instanceof Model) {
55-
throw new Exception("Class { $name_model } must be an instance of Illuminate\\Database\\Eloquent\\Model");
55+
throw new \Exception("Class { $name_model } must be an instance of Illuminate\\Database\\Eloquent\\Model");
5656
}
5757

5858
return $this->model = $model;
@@ -110,15 +110,15 @@ public function takeByWithRelations($limit, $field, $value)
110110
public function takeRandomByWithRelations($limit, $field, $value)
111111
{
112112
return $this->model->with($this->relations)->where($field, $value)
113-
->orderBy(DB::raw('RAND()'))->get()->take($limit);
113+
->orderBy(DB::raw('RAND()'))->get()->take($limit);
114114
}
115115

116116
/**
117117
* Return all rows with relations
118118
* @param array $columns
119-
* @param array $where
119+
* @return mixed
120120
*/
121-
public function allWithRelations($columns = array('*'), $where = array())
121+
public function allWithRelations($columns = array('*'))
122122
{
123123
return $this->model->with($this->relations)->get($columns);
124124
}
@@ -145,6 +145,18 @@ public function paginateWithAllRelations($perPage = 10, $columns = array('*'))
145145
return $this->model->with($this->relations)->paginate($perPage, $columns);
146146
}
147147

148+
public function paginateByFiltersWithAllRelations($filters = array(), $perPage = 10, $columns = array('*'))
149+
{
150+
$query = $this->model->with($this->relations);
151+
152+
foreach($filters as $field => $value) {
153+
$query->where($field, $value);
154+
}
155+
156+
return $query->get($columns);
157+
}
158+
159+
148160
/**
149161
* Create a record of model
150162
* @param array $data
@@ -185,11 +197,11 @@ public function delete($entity)
185197
{
186198
if(is_numeric($entity))
187199
{
188-
$this->model->destroy($entity);
200+
$this->model->destroy($entity);
189201
}
190202
else {
191-
$entity = $this->findOrFail($entity);
192-
$entity->delete();
203+
$entity = $this->findOrFail($entity);
204+
$entity->delete();
193205
}
194206
return true;
195207
}

src/RepositoryInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public function takeByWithRelations($limit, $field, $value);
1313

1414
public function takeRandomByWithRelations($limit, $field, $value);
1515

16-
public function allWithRelations($columns = array('*'), $where);
16+
public function allWithRelations($columns = array('*'));
1717

1818
public function paginate($perPage = 10, $columns = array('*'));
1919

2020
public function paginateWithAllRelations($perPage = 10, $columns = array('*'));
2121

22+
public function paginateByFiltersWithAllRelations($filters = array(), $perPage = 10, $columns = array('*'));
23+
2224
public function create(array $data);
2325

2426
public function update(array $data, $entity);

0 commit comments

Comments
 (0)