Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit e029976

Browse files
committed
Update Traits: ElasticSearchAuditable
1 parent b5741b3 commit e029976

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

src/Traits/ElasticSearchAuditable.php

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,18 @@
1919

2020
trait ElasticSearchAuditable
2121
{
22-
/**
23-
* @var string
24-
*/
25-
protected $client = null;
26-
27-
/**
28-
* @var string
29-
*/
30-
protected $index = null;
31-
32-
/**
33-
* @var string
34-
*/
35-
protected $type = null;
36-
37-
/**
38-
* ElasticSearch constructor.
39-
*/
40-
public function __construct()
41-
{
42-
parent::__construct();
43-
44-
$this->client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build();
45-
$this->index = Config::get('audit.drivers.es.index', 'laravel_auditing');
46-
$this->type = Config::get('audit.drivers.es.type', 'audits');
47-
}
48-
49-
public function esAudits($page = 1, $perPage = 10)
22+
public function esAudits($page = 1, $perPage = 10, $sort = 'latest')
5023
{
24+
$client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build();
25+
$index = Config::get('audit.drivers.es.index', 'laravel_auditing');
26+
$type = Config::get('audit.drivers.es.type', 'audits');
27+
5128
$from = ($page - 1) * $perPage;
29+
$order = $sort === 'latest' ? 'desc' : 'asc';
5230

5331
$params = [
54-
'index' => $this->index,
55-
'type' => $this->type,
32+
'index' => $index,
33+
'type' => $type,
5634
'size' => $perPage,
5735
'from' => $from,
5836
'body' => [
@@ -74,14 +52,14 @@ public function esAudits($page = 1, $perPage = 10)
7452
],
7553
'sort' => [
7654
'created_at' => [
77-
'order' => 'desc'
55+
'order' => $order
7856
]
7957
],
8058
'track_scores' => true
8159
]
8260
];
8361

84-
$results = $this->client->search($params);
62+
$results = $client->search($params);
8563
$hits = $results['hits'];
8664

8765
$collection = Collection::make();

0 commit comments

Comments
 (0)