This repository was archived by the owner on Nov 4, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +30
-7
lines changed Expand file tree Collapse file tree 5 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,9 @@ class ElasticUpdateMappingCommand extends Command
16
16
protected $ description = 'Update a model mapping ' ;
17
17
18
18
public function handle () {
19
- $ model = $ this ->getModel ();
19
+ if (!$ model = $ this ->getModel ()) {
20
+ return ;
21
+ }
20
22
21
23
$ configurator = $ model ->getIndexConfigurator ();
22
24
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ protected function getModel()
16
16
17
17
$ modelInstance = new $ modelClass ;
18
18
19
- if (!($ modelInstance instanceof Model) || !method_exists ( $ modelInstance , ' getIndexConfigurator ' )) {
19
+ if (!($ modelInstance instanceof Model) || !in_array (Searchable::class, class_uses_recursive ( $ modelClass ) )) {
20
20
$ this ->error (sprintf (
21
21
'The %s class must extend %s and use the %s trait. ' ,
22
22
$ modelClass ,
Original file line number Diff line number Diff line change @@ -24,16 +24,22 @@ public function __construct()
24
24
25
25
public function update ($ models )
26
26
{
27
- $ models ->map (function ($ model ) {
27
+ $ models ->each (function ($ model ) {
28
28
if ($ this ->updateMapping ) {
29
29
Artisan::call (
30
30
'elastic:update-mapping ' ,
31
31
['model ' => get_class ($ model )]
32
32
);
33
33
}
34
34
35
+ $ array = $ model ->toSearchableArray ();
36
+
37
+ if (empty ($ array )) {
38
+ return true ;
39
+ }
40
+
35
41
$ payload = (new DocumentPayload ($ model ))
36
- ->setIfNotEmpty ('body ' , $ model -> toSearchableArray () )
42
+ ->set ('body ' , $ array )
37
43
->get ();
38
44
39
45
ElasticClient::index ($ payload );
@@ -44,7 +50,7 @@ public function update($models)
44
50
45
51
public function delete ($ models )
46
52
{
47
- $ models ->map (function ($ model ) {
53
+ $ models ->each (function ($ model ) {
48
54
$ payload = (new DocumentPayload ($ model ))
49
55
->get ();
50
56
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class TypePayload extends IndexPayload
15
15
16
16
public function __construct (Model $ model )
17
17
{
18
- if (!method_exists ( $ model , ' getIndexConfigurator ' )) {
18
+ if (!in_array (Searchable::class, class_uses_recursive ( $ model ) )) {
19
19
throw new Exception (sprintf (
20
20
'The %s model must use the %s trait. ' ,
21
21
get_class ($ model ),
Original file line number Diff line number Diff line change 8
8
use \Exception ;
9
9
10
10
trait Searchable {
11
- use ScoutSearchable;
11
+ use ScoutSearchable {
12
+ ScoutSearchable::bootSearchable as bootScoutSearchable;
13
+ }
14
+
15
+ private static $ isSearchableTraitBooted = false ;
16
+
17
+ public static function bootSearchable ()
18
+ {
19
+ if (self ::$ isSearchableTraitBooted ) {
20
+ return ;
21
+ }
22
+
23
+ self ::bootScoutSearchable ();
24
+
25
+ self ::$ isSearchableTraitBooted = true ;
26
+ }
12
27
13
28
/**
14
29
* @return IndexConfigurator
You can’t perform that action at this time.
0 commit comments