Skip to content

Commit 593a92c

Browse files
committed
Fixed entity() base method compatibility
1 parent 8dfeba8 commit 593a92c

File tree

9 files changed

+34
-35
lines changed

9 files changed

+34
-35
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ composer require orkhanahmadov/eloquent-repository
3131

3232
Create a repository class and extend `Orkhanahmadov\EloquentRepository\EloquentRepository` abstract class.
3333

34-
Repository class that extends `EloquentRepository` must implement `entity` method. When using Eloquent models it's enough to return model's full namespace from the method.
34+
Repository class which extends `EloquentRepository` must implement `entity` method. When using Eloquent models it's enough to return model's full namespace from the method.
3535

3636
``` php
3737
namespace App\Repositories;
@@ -41,15 +41,7 @@ use Orkhanahmadov\EloquentRepository\EloquentRepository;
4141

4242
class UserRepository extends EloquentRepository
4343
{
44-
/**
45-
* Defines entity.
46-
*
47-
* @return mixed
48-
*/
49-
protected function entity()
50-
{
51-
return User::class;
52-
}
44+
protected $entity = User::class;
5345
}
5446
```
5547

@@ -82,9 +74,28 @@ class HomeController extends Controller
8274
}
8375
```
8476

77+
You can also skip creating dedicated repository class altogether,
78+
instead inject `Orkhanahmadov\EloquentRepository\EloquentRepository` and set Eloquent model entity dynamically.
79+
80+
``` php
81+
namespace App\Http\Controllers;
82+
83+
use App\User;
84+
use Orkhanahmadov\EloquentRepository\EloquentRepository;
85+
86+
class HomeController extends Controller
87+
{
88+
public function index(EloquentRepository $repository)
89+
{
90+
return $repository->setEntity(User::class)->get();
91+
}
92+
}
93+
```
94+
95+
8596
### Available methods
8697

87-
Extending `EloquentRepository` class offers has many familiar shortcut methods from Eloquent.
98+
Eloquent Repository class offers has many familiar shortcut methods from Eloquent.
8899

89100
**Create a model:**
90101
``` php

src/Console/stubs/repository.model.stub

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,5 @@ use Orkhanahmadov\EloquentRepository\EloquentRepository;
77

88
class DummyClass extends EloquentRepository
99
{
10-
/**
11-
* Defines entity.
12-
*
13-
* @return mixed
14-
*/
15-
protected function entity()
16-
{
17-
return DummyModelClass::class;
18-
}
10+
protected $entity = DummyModelClass::class;
1911
}

src/Console/stubs/repository.stub

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,5 @@ use Orkhanahmadov\EloquentRepository\EloquentRepository;
66

77
class DummyClass extends EloquentRepository
88
{
9-
/**
10-
* Defines entity.
11-
*
12-
* @return mixed
13-
*/
14-
protected function entity()
15-
{
16-
// TODO: specify model
17-
}
9+
// protected $entity = Model::class;
1810
}

src/EloquentRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct(Application $application, Cache $cache)
6868
*
6969
* @throws BindingResolutionException
7070
*/
71-
public function entity($entity): self
71+
public function setEntity($entity): self
7272
{
7373
$this->entity = $entity;
7474
$this->resolveEntity();

src/Repository/Concerns/CreatesEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* @property-read Builder|Model $model
10+
* @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
1011
*/
1112
trait CreatesEntity
1213
{

src/Repository/Concerns/DeletesEntity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
/**
1111
* @property-read Builder|Model $model
12-
* @method Builder|Model find(int $modelId)
13-
* @method void invalidateCache()
12+
* @method Builder|Model find($modelId)
13+
* @method void invalidateCache($model)
14+
* @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
1415
*/
1516
trait DeletesEntity
1617
{

src/Repository/Concerns/SelectsEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @property-read Factory $cache
1515
* @method string cacheKey()
1616
* @method int cacheTTLValue()
17+
* @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
1718
*/
1819
trait SelectsEntity
1920
{

src/Repository/Concerns/UpdatesEntity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable;
88

99
/**
10-
* @method Builder|Model find(int $modelId)
11-
* @method void invalidateCache()
10+
* @method Builder|Model find($modelId)
11+
* @method void invalidateCache($model)
12+
* @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
1213
*/
1314
trait UpdatesEntity
1415
{

tests/Repository/EloquentRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testCreate()
2323
{
2424
$this->assertCount(0, Model::all());
2525

26-
$result = $this->repository->entity(Model::class)->create(['id' => 5, 'name' => 'model name']);
26+
$result = $this->repository->setEntity(Model::class)->create(['id' => 5, 'name' => 'model name']);
2727

2828
$this->assertEquals(5, $result->id);
2929
$this->assertEquals('model name', $result->name);

0 commit comments

Comments
 (0)